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?
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?
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 repeat wait loop which will basically wait until your players character was 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 torso to 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 localplayer by 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.
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
thank you for ur help
This discussion is preserved as it was. New answers, comments, and votes are closed.
5 comments
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.
Very true, thanks for pointing that out to me.
it didn't work for my game why?
You can't use localplayer in a script. Only in a localplayer and make sure the parenting is correct
okey I solved problem thx