Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

3 comments

0

LocalPlayer is nil on the server.

0

oof. Is there any other way to reference the players name?

0

Answered

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.