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

why is this broken?

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)

2 comments

1

On line 9, use FindFirstChild or square brackets. Ex, game.Workspace.Players:FindFirstChild(plr2.Name):Destroy()

0

Square brackets = [ ]

1 answer

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.

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