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

Why does my teleport script work in the studio but not in the game?

Asked by 6 years ago
Edited 6 years ago

I have a loading screen and when you click "Load Game" it should teleport you to a game. But the problem is, it gives me an error saying

"Invalid player to teleport" when I play it in game but when I try it in studio, it works perfectly fine

script

par = script.Parent.Parent
function onClicked()
    script.Parent:Destroy()
    par["Start Game"]:Destroy()
    par.ImageLabel.Image = "rbxgameasset://Images/Orange"
    game:GetService("TeleportService"):Teleport(961702140, game.Players.LocalPlayer)

end


script.Parent.MouseButton1Click:connect(onClicked)
0
is it a local script? viltsuviltsu 4 — 6y
0
oh wait.... XD it's a server script SmugNyan 24 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

You do not need to add the game.Players.LocalPlayer . You could just use this in a LocalScript:

local par = script.Parent.Parent --Since this variable is used within a function, its best to add local to it.
script.Parent.MouseButton1Down:connect(function()
    script.Parent:Destroy()
    par["Start Game"]:Destroy()
    par.ImageLabel.Image = "rbxgameasset://Images/Orange"
    game:GetService("TeleportService"):Teleport(961702140) --Notice the small change
end)

If this worked, please upvote and accept answer. If it didn't work, please comment. Thanks!

0
game.Players.LocalPlayer is better to use then script.Parent.Parent becuase if you need help pro scripters will have no idea what the parent is or anything like that and its just a hasle to change if its in a diff child, so I would recomend using game.Players.LocalPlayer. outlook1234567890 115 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

You should use an event. Not sure if you should make it a local script but here's the code.

Local par = script.Parent.Parent
Local plr = game.Players.LocalPlayer

Script.parent.MouseButton1Click:connect(function(click)

Script.Parent:Destroy()
Par["Start Game"]:Destroy()

par.ImageLabel.Image = "rbxgameasset://Images/Orange"

    game:GetService("TeleportService"):Teleport(961702140, plr)

End)

Try this and tell me if it doesn't work try adding game.Players.PlayerAdded:connect(function(plr) before the first event and removing the plr variable.

0
Also make "End)" all lowercase AwesomeBuilder346 11 — 6y
0
Your answer is error ridden. Please look it over and correct it. PyccknnXakep 1225 — 6y

Answer this question