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

Trouble Finding Value from LocalPlayer in a ServerScript?

Asked by 2 years ago

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.

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? RegionalNatalie 5 — 2y
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. NotThatFamouss 605 — 2y
0
That is the only line RegionalNatalie 5 — 2y
0
Can you explain what you are trying to make? NotThatFamouss 605 — 2y
0
A nametag that has the players last name from a value in the LocalPlayer. RegionalNatalie 5 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

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
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? RegionalNatalie 5 — 2y
0
Easy! You just have to use the Changed or GetPropertyChangedSignal Event. NotThatFamouss 605 — 2y
Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
2 years ago

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 RemoteEventhooked up in a LocalScriptinside 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! :)

Answer this question