传送到出生点脚本

有人知道这个脚本有什么问题吗?脚本应该是如果你的时间倍率比你的时间大750倍,那么传送按钮就会变成可见的,这部分是有效的,但是当它到达你点击的位置时停止工作,开发控制台中没有出现任何错误。我认为发生的是在这行代码后事件不会触发,因为我尝试打印之后没有任何东西被打印出来,所以我认为事件没有被触发。当你点击按钮时,文本应该显示3、2、1,然后重新回到原来的样子,并且你会被传送到出生点,如果你移动了你的角色,那么它会重置,文本会回到正常状态,它不会传送你,并且你必须再次点击它并一直保持静止以传送你。此外,pog是一个boolvalue,我把它添加到了玩家中,默认值为false,当你进入时,它的值是存在的。

代码在文本按钮中的本地脚本中,在startergui中有一个screengui:

player = game.Players.LocalPlayer
character = game.Workspace[player.Name]

while true do
    wait(1)
if player.leaderstats.Time.Value < player.TimeMulti.Value*750 then
    script.Parent.Visible = false
elseif player.leaderstats.Time.Value >= player.TimeMulti.Value*750 then
        script.Parent.Visible = true
    end
end
local pog = player.pog.Value

script.Parent.MouseButton1Click:Connect(function()
         pog = true
    while pog == true do
        wait (1)
        character.Humanoid.Changed:Connect(function()
    if character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "Teleport to spawn"
then
            script.Parent.Text = "3"
    elseif character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "3" then
            script.Parent.Text = "2"
    elseif character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "2" then
            script.Parent.Text = "1"
    elseif character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "1" then
        character.Torso.CFrame = game.Workspace.Spawn.SpawnLocation.CFrame
            script.Parent.Text = "Teleport to spawn"
    elseif character.Humanoid.MoveDirection.Magnitude ~= 0 then
            script.Parent.Text = "Teleport to spawn"
         pog = false
        end
    end)
    end
end)
点赞
用户13065801
用户13065801

问题

你在 while 循环下连接了按钮,脚本在 while 循环结束前等待,然后才分配按钮点击事件,但是循环永远不会结束,所以没有连接。

解决方法

只需将 .MouseButton1Click 和变量 pog 事件放在 while 循环之前。(将第 12 行及其以下所有内容移动到 while 循环之前)。

如果您有任何问题,请随时提问。如果这解决了您的问题,请确保将其标记为答案,以便其他人也能从中受益。

代码

player = game.Players.LocalPlayer
character = game.Workspace[player.Name]

local pog = player.pog.Value

script.Parent.MouseButton1Click:Connect(function()
         pog = true
    while pog == true do
        wait (1)
        character.Humanoid.Changed:Connect(function()
    if character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "Teleport to spawn"
then
            script.Parent.Text = "3"
    elseif character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "3" then
            script.Parent.Text = "2"
    elseif character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "2" then
            script.Parent.Text = "1"
    elseif character.Humanoid.MoveDirection.Magnitude == 0 and script.Parent.Text == "1" then
        character.Torso.CFrame = game.Workspace.Spawn.SpawnLocation.CFrame
            script.Parent.Text = "Teleport to spawn"
    elseif character.Humanoid.MoveDirection.Magnitude ~= 0 then
            script.Parent.Text = "Teleport to spawn"
         pog = false
        end
    end)
    end
end)

while true do
    wait(1)
if player.leaderstats.Time.Value < player.TimeMulti.Value*750 then
    script.Parent.Visible = false
elseif player.leaderstats.Time.Value >= player.TimeMulti.Value*750 then
        script.Parent.Visible = true
    end
end

另外,我强烈建议在 .MouseButton1Click 函数下使用 repeat 而不是 while 循环。

2021-04-08 18:44:14