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

why is this broken?

Asked by 7 years ago
Edited 7 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I have a string value that was inserted into the workspace called "Players". When a player is added into the game it create another string value having the parent be "Players" However it keeps giving this output and will not work. 17:31:19.224 - Workspace.PLR:9: attempt to call field 'Players' (a userdata value).

game.Players.PlayerAdded:connect(function(plr)
    wait(.5)
    local k = Instance.new("StringValue",game.Workspace.Players)
    k.Name = plr.Name
    k.Value = plr.Name
end)
game.Players.PlayerRemoving:connect(function(plr2)
    if game.Workspace.Players:FindFirstChild(plr2.Name) then
        game.Workspace.Players(plr2.Name):remove()
    end
end)
1
On line 9, use FindFirstChild or square brackets. Ex, game.Workspace.Players:FindFirstChild(plr2.Name):Destroy() User#11440 120 — 7y
0
Square brackets = [ ] StoIid 364 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

On line 9, use FindFirstChild or square brackets.

Ex, game.Workspace.Players:FindFirstChild(plr2.Name):Destroy() or game.Workspace.Players[plr2.Name]:Destroy(). I'll be using FindFirstChild.

Your code should look somewhat like this,

-- regular script
game.Players.PlayerAdded:connect(function(plr)
    wait(.5)
    local k = Instance.new("StringValue",game.Workspace.Players)
    k.Name = plr.Name
    k.Value = plr.Name
end)
game.Players.PlayerRemoving:connect(function(plr2)
    if game.Workspace.Players:FindFirstChild(plr2.Name) then
        game.Workspace.Players:FindFirstChild(plr2.Name):Destroy()
    end
end)
I also changed remove() to Destroy() because remove() is deprecated.

Good Luck

If I helped you, please don't forget to accept my answer.
Ad

Answer this question