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

Teleport script only works in studio and never in game, what am I doing wrong?

I'm trying to make a script that teleports someone who clicks a button, but it never works in game it only works in studio. What did I do wrong?

function onClicked(playerWhoClicked)
	hum = game.Players.LocalPlayer.Character
	hum.Torso.CFrame = CFrame.new(script.Parent.Parent.Teleport.Position) 
end 

script.Parent.ClickDetector.MouseClick:connect(onClicked)

7 comments

0

Is this in a local script or a server script?

0

It's a server script.

1

Since you Used LocalPlayer for your "hum" Variable, you will need to try it in a local script since LocalPlayer is used in Local Scripts.. Im not sure if it will work, but see if it will

0

It doesn't work. Is the problem the LocalPlayer? I wasn't sure how else to find the player who clicked it.

0

Look, I gtg so I cant really help you right now, but Local scripts do not work in Workspace, and I believe your problem is using Local Player, so you will need probably need to access the player another way, I will be back on tomorrow to see if you figured it out, then I can help then..

0

Okay, I'll try and figure it out

0

I edited my question.

2 answers

✓ Accepted answer

[Edited] You should use the player value that is in the function.. Here is your script using it. Also, I edited some stuff that is unnecessary.

script.Parent.ClickDetector.MouseClick:connect(function(plr) --Plr is a short way to say player. It makes writing your scripts easier. 
	game.Workspace[plr.Name].Torso.CFrame = CFrame.new(script.Parent.Parent.Teleport.Position) --I am just saying, but you should use the head to make it R15 compatible. 
end)

2 comments

0

That didn't work either. The two error messages were "attempt to call a nil value" and "Torso is not a valid member of Player" if that helps at all.

0

The edited version worked! Thanks!

Put this inside of the button script

local spawn = game.Workspace.TeleportLocation -- make a brick in the Workspace name 'TeleportLocation'.
player = script.Parent.Parent.Parent.Parent.Parent.Parent  -- if this script doesn't work, look at this line. Make sure the final Parent is Workspace.

function onClicked()
	player.Character.Torso.CFrame = CFrame.new(spawn.Position)
end

script.Parent.MouseButton1Down:connect(onClicked)

Accept answer and up vote if this works, thank!

1 comment

0

It still isn't working, the error message is "Character is not a valid member of Workspace" if that helps.

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