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

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

Asked by 3 years ago

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

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

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

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. NGC4637 602 — 3y
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. DocGooseYT 110 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
LocalPlayer only works on local scripts. It will error in scripts NGC4637 602 — 3y
0
OnServerEvent only works on scripts. So theres a good chance it is a script. NGC4637 602 — 3y
0
Fixed it, he just has to make sure it's a child of the player's character unless he wants to change it. Velcorii 13 — 3y

Answer this question