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

Trouble Finding Value from LocalPlayer in a ServerScript?

So I have made it so the LocalPlayer gets 3 values, the main one I am focusing on is "LastName"

So I made this script in a ServerScript for a belt that you can put on;

script.Parent.Text = game.Players.LocalPlayer.CharacterName.Value

This appears to not find the values value and put it on the text, I assume there is meant to be a different script solution as to this is a ServerScript and I have gotten reference on what to do from my LocalScripts that have the similar line of code.

If I need to clear anything up with confusion on what I am explaining please let me know.

5 comments

0

I had realized ServerScripts can't access LocalPlayers, would there be any work around, maybe a suggested line of code for firing a remotevent?

0

Is this the only line of code or is there other lines? Because we cannot really help you if we don't have a full understanding on what you're trying to create.

0

That is the only line

0

Can you explain what you are trying to make?

0

A nametag that has the players last name from a value in the LocalPlayer.

2 answers

You can use PlayerAdded as an alternative to LocalPlayer if you're using a server script.

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.Text = plr:WaitForChild("CharacterName").Value
end)

Or you can just use the plr.Name instead of the CharacterName to get the Player's name, unless if you are making something else.

Or, if the tag is a descendant of a player's character, then:

local Char = script.Parent.Parent.Parent -- // Assuming the TextLabel is a Child of a SurfaceGui or BillboardGui, then the Gui is a Child of the Character.
local plr = game.Players:GetPlayerFromCharacter(Char)

script.Parent.Text = plr:WaitForChild("CharacterName").Value

2 comments

0

That seemed to have worked! My only problem is, it doesn't update when the value in the player is updated, because right now we don't have it so it saves the value when you exit, so you have to enter the value new every time you join, would there be a solution to it being able to update?

0

Easy! You just have to use the Changed or GetPropertyChangedSignal Event.

Hmm... think i understand what you're trying to do here. You're trying to get the players name for your server script to display it in your belt?

If so, I've written a little example on how you can use a RemoteEvent hooked up in a LocalScript inside the player and a server-side script that listens to that event...

Code:

ServerScript:

local Rs = game:GetService("ReplicatedStorage")
local REvent = Rs.RemoteEvent

REvent.OnServerEvent:Connect(function(player)
	print("Player[",player.Name,"] Has Called me!")
end)

Local Script: (using a tool to activate this event!..)

local Rs = game:GetService("ReplicatedStorage")
local REvent = Rs.RemoteEvent
local Tool = script.Parent

Tool.Activated:Connect(function() 
	REvent:FireServer()
end)

If you want to pass more than the player to your server side script you just add it like this:

ServerScript:

local Rs = game:GetService("ReplicatedStorage")
local REvent = Rs.RemoteEvent

REvent.OnServerEvent:Connect(function(player, Val1--[[,ETC...]])
	print("Player[",player.Name,"] Has Called me! \n Val1 is [",Val1,"]")
end)

Local Script: (using a tool to activate this event!..)

local Rs = game:GetService("ReplicatedStorage")
local REvent = Rs.RemoteEvent
local Tool = script.Parent

Tool.Activated:Connect(function() 
	REvent:FireServer("Test!"--[[,ETC...]])
end)

Hope this helps! :)

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