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

How to teleport players?

Asked by 9 years ago

function onButtonClicked(player) game.Workspace.Player.Torso.CFrame = CFrame.new(Vector3.new(-24, 332.6, 134)) end

script.Parent.MouseButton1Click:connect(onButtonClicked)

this script working on studio testing but not working on roblox player why?

2 answers

Log in to vote
1
Answered by
Thetacah 712 Moderation Voter
9 years ago

First things first, the MouseButton1Click event doesn't take any parameters...Lets start out by removing that parameter.

In this script, you're attempting to move a player named 'Player''s CFrame to a CFrame location. This would only work if the player onlines name would be 'player'. It's possible that the user player visits your game, but probably not haha.

We can make a player variable by parenting from the gui. To parent it correct, make sure that the player variable is set to StarterGui's parent.(Everything in StarterGui gets transferred into this folder called PlayerGui that's situated in the player. The scripts actually execute in the players playergui, not from startergui. Keep that in mind.)

In my case, this would be what the player variable would look like.(Change the parenting to suit your needs)

local player = script.Parent.Parent.Parent.Parent

I then used a repeatwait loopwhich will basically wait until your players characterwas there.(I know this isn't very efficient, but it's okay.) I then made a variable for the character once we knew it was there. I waited for the torsoto be there, as well.

This should be what the final product should look like...


local player = script.Parent.Parent.Parent.Parent function onButtonClicked() repeat wait() until player.Character local character = player.Character character:WaitForChild("Torso").CFrame = CFrame.new(Vector3.new(-24, 332.6, 134)) end script.Parent.MouseButton1Click:connect(onButtonClicked)

Also, as Perci1 has said, if this is in a localscript, you can use localplayerby defining it like this:

local player = game.Players.LocalPlayer

LocalPlayer is, well, the localplayer. It would be the player in which is running the script. Think of it like that.

0
Nothing inefficient about waiting for a character to exist, although in this case it's probably unnecessary since you're already waiting for the torso to exist. Perci1 4988 — 9y
0
Very true, thanks for pointing that out to me. Thetacah 712 — 9y
0
it didn't work for my game why? lugat2001 5 — 9y
0
You can't use localplayer in a script. Only in a localplayer and make sure the parenting is correct Thetacah 712 — 9y
0
okey I solved problem thx lugat2001 5 — 9y
Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Well first, your player parameter will just equal nil because the MouseButton1Click event doesn't have any built in parameters.

Your main problem, however, is the line

game.Workspace.Player.Torso.CFrame = CFrame.new(Vector3.new(-24, 332.6, 134))

and specifically,

game.Workspace.Player

The dot, ., searches for objects or properties by name. If a child or property doesn't exist with the name you provided, your script will throw an error.

In PlaySolo, this script works because your Player is, by default, named 'Player'. In the actual game, however, no one is going to have a username of 'Player'. Since your searching for 'Player' by name, the script will error.

If this is a local script (as it should be -- you're working with GUIs), then you can access the player who clicked by using

plr = game.Players.LocalPlayer
--LocalPlayer is a PROPERTY, used by LocalScripts to access the Player object of the specific computer they're running on.

If this is a server script, you can 'parent up' to the specific Player. The script, when placed inside a GUI, will always be a descendant of PlayerGui. PlayerGui is a direct child of Player. You can therefore just use repeating Parents

plr = script.Parent.Parent.Parent.Parent
--The exact number of Parent's will vary. I used 4 here because it is the most common number, used with the hierarchy of Player>PlayerGui>ScreenGui>TextButton>Script
0
thank you for ur help lugat2001 5 — 9y

Answer this question