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?
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.