【发布时间】:2021-09-18 16:09:58
【问题描述】:
我是 Roblox 开发的新手,但想尝试制作 Asteroids 游戏。我开始为宇宙飞船创建座位,但我发现了一些我无法解释的行为,希望有人能提供一些澄清。
这是我正在谈论的代码,只是一些非常基本的东西,因此我可以掌握一些开始的想法:
--Services--
local Players = game:GetService("Players")
--Local Variables--
local seat = script.Parent -- Refers to the VehicleSeat object
local currentPlayer = nil -- Prevents errors in reference to nil?
local prompt = script.Parent.GetInPrompt -- Get the parent object's proximity prompt
--Proximity Prompt Trigger Code--
prompt.Triggered:Connect(function(currentPlayer) -- Bind the prompt trigger to function
local char = currentPlayer.Character -- Get the currentPlayer
seat:Sit(char.Humanoid) -- Make them sit
prompt.Enabled = false -- Disable the prompt so it isn't visible
print("Character is sitting") -- Print to console for debug
end)
--Get up function--
local function gettingUp(currentPlayer)
print("Attribute Change Fired - Getting Up") -- Print to console for debug
prompt.Enabled = true
end
-- Note that since Lua is an interpreted language any function must be defined before use--
seat.ChildRemoved:Connect(function()
gettingUp(currentPlayer)
end)
我看到我访问了顶部的“Players”服务,并将 currentPlayer 变量初始化为 nil。我不明白在proximityPrompt 触发时currentPlayer 如何从nil 变为播放器。
我认为这可能是 Roblox 中的“保留名称”,但我在网上找不到任何证据。
【问题讨论】: