Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't a player teleport when the time expires?

Asked by 8 years ago

The script is supposed to teleport a player to the position (4, 1, 7) when the timer reaches "Time Expired". No errors show up in the output window. I don't know if i'm using the if and then statement wrong.

Script 1: Located in StarterGui->Countdown->Timer

local time = script.Parent

time.Text = "10"
wait(1)
time.Text = "9"
wait(1)
time.Text = "8"
wait(1)
time.Text = "7"
wait(1)
time.Text = "6"
wait(1)
time.Text = "5"
wait(1)
time.Text = "4"
wait(1)
time.Text = "3"
wait(1)
time.Text = "2"
wait(1)
time.Text = "1"
wait(1)
time.Text = "Time Expired"
print("Finished Countdown")

Script 2: Located in ServerScriptService

local RoundClock = game.StarterGui.Countdown.Timer

if RoundClock.Text == "Time Expired" then
    wait(2)
    game.Workspace.Player.Torso.CFrame = CFrame.new(Vector3.new(4, 1, 7))
end

1 answer

Log in to vote
2
Answered by 8 years ago

Problem #1:

StarterGui does not actively change anything, it is simply where guis are cloned from, and will not make the changes you need.

To change a player's gui, go to PlayerObject.PlayerGui.

I will rewrite your time script:

for _,v in pairs (game.Players:GetPlayers()) do
    v.PlayerGui.Countdown.Timer.Text = "10"
end
wait(1)
-- repeat

Problem #2:

Your timer script is inefficient, try using a loop:

local num = 10 -- how many seconds
while num > 0 do
    wait(1)
    num = num - 1
end
-- end code

Problem #3:

Not really a problem, but a better way to do your 2nd script will be:

if script.Ended.Value == true then -- create a bool value called 'Ended' inside the script
    teleport() -- to do the equivalent of my answer on Problem #1
end

I recommend you use functions to simplify all of your code, so:

function updateGui()
    -- code
end

function teleport()
    -- code
end

-- etc.

Final Scripts

Script #1:

num = 10

function updateGui()
    for _,v in pairs (game.Players:GetPlayers()) do
        v.PlayerGui.Countdown.Timer.Text = num
    end
end

while num > 0 do
    wait(1)
    updateGui()
end

game.Workspace.Script2.Ended.Value = true

Script #2:

function teleportPlayers()
    for _,v in pairs (game.Players:GetPlayers()) do
        v.Character.Torso.CFrame = CFrame.new(Vector3.new(4, 1, 7))
    end
end

script.Ended.Changed:connect(function()
    if script.Ended.Value == true then
        script.Ended.Value = false
        teleportPlayers()
    end
end

Hope I helped :)

~TDP

0
Thank you so much! User#210 0 — 8y
Ad

Answer this question