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

Why won't my teleport button script work?

function onClicked () 
    local player = game.Players.LocalPlayer
        if player then
        game:GetService("TeleportService"):Teleport(283976037, player)
    end
end
game.StarterGui.ScreenGui.TextButton.MouseButton1Down:connect(onClicked)

Honestly this is probably "my" first "complicated" script.....

1 answer

✓ Accepted answer

Well first, you have the arguments inverted for the "Teleport" function. The "player" variable should be first, and the place id second. Like this:

function onClicked () 
	local player = game.Players.LocalPlayer
	if player then
		game:GetService("TeleportService"):Teleport(player, 283976037)
	end
end
game.StarterGui.ScreenGui.TextButton.MouseButton1Down:connect(onClicked)

And as TheAlphaStigma pointed out, you're trying to set an event to something in the StarterGui service, which does not automatically update to individual players. You should use the ancestry chain of the script, by using the "Parent" property.

For example:

-- Also make sure this is a local script, since we're getting the LocalPlayer from the Players service.

function onClicked () 
	local player = game.Players.LocalPlayer
	if player then
		game:GetService("TeleportService"):Teleport(player, 283976037)
	end
end

-- Assuming this script is inside the TextButton GUI, this would work.

script.Parent.MouseButton1Down:connect(onClicked)

2 comments

0

Although that is one of the problems, take a look at the code again (hint: Line 7). :)

0

Yes, there's also that.

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