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

How do you get a player name in a server script?

In my code I have a door, when click it shows there player name on the door and lets them go through it. Right now it doesn't work in multiplayer but it will run in test mode. It currently passes (player) from a local script.

door = game.Workspace.Door88

game.Workspace.Doors.Door88Event.OnServerEvent:Connect(function(player)
	if game.Players.LocalPlayer.PlayerGui.Values.DoorCheck.Value == false then
		if door.Owner.Value == "Unowned" then
			door.Owner.Value = player.Name
			door.SurfaceGui.Owner.Text = door.Owner.Value
			game.Players.player.Name.PlayerGui.Values.DoorCheck.Value = true
		end
	elseif game.Players.player.Name.PlayerGui.Values.DoorCheck.Value == true then
		if door.Owner.Value == player.Name then
			door.Owner.Value = "Unowned"
			door.SurfaceGui.Owner.Text = "Unowned"
			game.Players.player.Name.PlayerGui.Values.DoorCheck.Value = false
		else
			print("Door Owned")
		end
	end
end)

I was wondering how I would be able to change LocalPlayer in line 4 to get a players name. Any suggestions. Thanks.

2 comments

0

player in the parameter is what should be used, don't do game.Players.LocalPlayer since that only used in local script (LocalPlayer) .

0

user = game.Players:findFirstChild(Tool.Parent.Name) if ur good at scripting try making this into whenver u click the door

1 answer

✓ Accepted answer

Here's a quick example.

-- in clickdetector

script.Parent.MouseClick:connect(function(player)
	workspace.RemoteEvent:FireServer(player.Name)
end)

-- in remoteevent

script.Parent.OnServerEvent:connect(function(player)
	print(player)
end)

Basically, what I'm doing is sending the RemoteEvent the name of the player FROM the FireServer.

In your case, you just define "player". So, it'd be something like this.

-- assuming you're using clickdetector

script.Parent.MouseClick:connect(function(player)
	workspace.Doors.Door88Event:FireServer(player.Name)
end)

-- remoteevent

game.Workspace.Doors.Door88Event.OnServerEvent:connect(function(player)
    if game.Players:FindFirstChild(player).PlayerGui.Values.DoorCheck.Value == false then -- finding the player using the name, that's why I added that part.
-- etc
-- etc
-- and so on

1 comment

0

for the clickdetector, you can also just do :FireServer(player) and find the player.Name later in the remote event script.

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