Can I find a LocalPlayer from a ServerScript by their name?

I have the player’s name already stored in a StringValue named “Owner”. I’m trying to find the player by finding if “Players” has a child with the corresponding name. I’m doing this so I can send a :FireClient() across.

local Health = script.Parent.Health
local Remote = script.Parent.HealthChanged

local OwnerName = script.Parent.Owner.Value
local Players = game:GetService("Players")
local Player = Players:FindFirstChild(OwnerName)

Health.Changed:Connect(function(Value)
	print("Health Changed to "..Value.."/100")
	Remote:FireClient(Player, Value)
end)

This should work, does it not work? If it doesn’t, does it error/can you print OwnerName?

1 Like

Alright I’ll run it again and tell you the output.

I’m receiving a n error message saying “FireClient: player argument must be a Player object” and when I use

print(OwnerName)

It prints a blank value.

Actually, I think I could solve it by allowing the script to update the value “Owner” because that’s another value that is changed by the server.

1 Like

Since you’re changing the value after you first set it as a variable, your variable always stores the original value. local OwnerName = script.Parent.Owner.Value doesn’t update when the value of owner updates. If you want it to update, you’d need to do something like local Owner = script.Parent.Owner and look for a player (players:FindFirstChild(Owner.Value)) every time you want to fire.

You should also check if the player exists before firing the event so it doesn’t error like that. You can just do something simple like

if Player then
     Remote:FireClient(Player, Value)
end
1 Like

I’ve changed the default value of Owner to “Owner” and I tried to find if the value was changed

local Owner = script.Parent.Owner
local OwnerName = Owner.Value
if OwnerName == "Owner" then
    print("Empty Value")
    Owner.Changed:Connect(function(Name)
	    OwnerName = Name
    end)
end

No luck though.

You can just check Owner.Value every time you need to use the value instead of storing it as a separate variable.

1 Like

Instead of storing the name in a string value couldn’t you store the Player object in an object value?

2 Likes

Instead of using ValueObjects, couldn’t you just use a ModuleScript to store this information? It avoids the cost of instances in the DataModel and makes modification fairly quick.