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

Teleport script works in studio, not in game. Any help? [SCRIPT in ServerScriptService]

Asked by 5 years ago

So i'm making a demo game, where whenever you join, it waits 5 seconds, then teleports you to 1 of 2 blocks. Then five seconds later, it does it again. This works in Studio, but not in the Game. Here is the script: (This is a SCRIPT [not local] in ServerScriptService)

while true do
    local Tab = {CFrame.new(26, 0.5, -1); CFrame.new(26, 0.5, -33)}
    local locationselected = Tab[math.random(1, #Tab)]
    wait(.2)
    game.Workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = locationselected
    wait()
end

i tried adding a player added event:

game.Players.PlayerAdded:Connect(function(player)
while true do
    local Tab = {CFrame.new(26, 0.5, -1); CFrame.new(26, 0.5, -33)}
    local locationselected = Tab[math.random(1, #Tab)]
    wait(.2)
    game.Workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = locationselected
    wait()
end
end)

but thats not working either. Any help?

0
LocalPlayer is nil on the server. User#19524 175 — 5y
0
oof. Is there any other way to reference the players name? SBlankthorn 329 — 5y
0
Answered Operation_Meme 890 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You cannot get LocalPlayer from a server script. To fix this, use a PlayerAdded event and use a loop.

Here is a fixed script:

local Tab = {CFrame.new(26, 0.5, -1); CFrame.new(26, 0.5, -33)}

game.Players.PlayerAdded:Connect(function(player)
    spawn(function()
        while wait(0.2) do
            local locationselected = Tab[math.random(1, #Tab)]
            player.Character:MoveTo(locationselected.p)
        end
    end)
end)

If this worked, don't forget to accept my answer and comment if you have any questions.

Ad

Answer this question