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

How can I get my script to get a player's character?

So, I'm trying to make it so a player can tp somewhere by clicking a button. But, I just can't figure out how to get the script to find out who the player is. I tried using "LocalPlayer" but that gets the player in "Players" service not in workspace. What do I do?

local Player = workspace.LocalPlayer

game.ReplicatedStorage.BackToLobby.OnServerEvent:Connect(function()
	Player.Character.HumanoidRootPart.CFrame = CFrame.new(-44.093, 3.225, 35.84)
end)

2 answers

✓ Accepted answer

funny thing,

there is a parameter in OnServerEvent, which references the player who fired the event. so you can just do this:

local RS = game:GetService("ReplicatedStorage")

RS.BackToLobby.OnServerEvent:Connect(function(player)
	player.Character.HumanoidRootPart.CFrame = CFrame.new(-44.093, 3.225, 35.84)
end)

tips on OnServerEvent

2 comments

0

Also, you cannot reference local player in a server script, it will just error since the server ain't a player it's a server.

0

Yeah I know, I was just trying it to see if it would work lol. Anyways thanks for the answer, I actually already figured it out which was just going back to the local script and using "player.Character" but the info you gave me is also pretty useful so I might use this instead since it isn't a local script.

To get the players character you change

local Player = workspace.LocalPlayer

To this

local Player = script.Parent

Then the script should be

local Player = script.Parent

game.ReplicatedStorage.BackToLobby.OnServerEvent:Connect(function()
    Player.HumanoidRootPart.CFrame = CFrame.new(-44.093, 3.225, 35.84)
end)

3 comments

0

LocalPlayer only works on local scripts. It will error in scripts

0

OnServerEvent only works on scripts. So theres a good chance it is a script.

0

Fixed it, he just has to make sure it's a child of the player's character unless he wants to change it.

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