I can't get a string, need help!

Hi. I’m looking for assistance with my script. Everything works except the playername, it keeps coming back Nil. I’m looking to create the data that came from the RemoteFunction (plr) and turn it into a string so I can put it into a StringValue.

script.clonep.OnServerInvoke = function(plr,pris)
print(pris.Name)

local playername = game.Players:FindFirstChild(plr).Name
print(playername)


local LOL = script.Parent.Handcuffs.P:Clone()
LOL.Police.Value = plr
LOL.OfficerString.Value = playername
LOL.Script.Disabled = false
LOL.Parent = pris.Character
LOL.Disabled=false
return LOL

end

Please help.

The error is: [18:23:46.457 - Players.Player2.Backpack.Handcuffs.Script:5: attempt to index a nil value]

1 Like

Nevermind, fixed it. I can’t believe I didn’t spot it at first lol.

I do not know why you don’t just do plr.Name here?

local playername = game.Players:FindFirstChild(plr).Name
print(playername)

Spot what…?

If plr is an object, then there is no need to reference it by game.Players… because the object is already known (its parent, its properties, etc.) within the variable.

Simply setting “playername” to plr.Name would also work.
Just thought I’d let you know, so that you script more efficiently.

2 Likes

FYI:

local playername = game.Players:FindFirstChild(plr.Name).Name

Is fine to do however you don’t need to index it through the Players service, you can just do:

local playername = plr.Name
1 Like

I believe we thought exactly the same thing.

1 Like

That’s what I did, felt so stupid after I realised it.