String Value are not being removed

So I am making a Round System based game and things are not going as I am planning.
There are errors everywhere I debugged almost everything except two things.

This one is important

I have a string value which will be inserted to a random player.If a player has that string value they would recieve a powerful weapon more powerful than others.

So I scripted it and it was working fine but the problem is the String Value is not being removed when the Round is over

Here is the script -

for x,v in pairs(plrs) do
	local king = plrs[math.random(1, #plrs)] 
	
	local KingTag = Instance.new("StringValue")
	KingTag.Parent = king
	KingTag.Name = "King"
	
	local KingTools = ServerStorage:WaitForChild("KingsTools"):GetChildren()
	
	if player:FindFirstChild("King") then
		local RadWeapon = KingTools[math.random(1, #KingTools)]
		
		local ClonedWeapon = RadWeapon:Clone()
		ClonedWeapon.Parent = king.Backpack
	end
end
-- a few lines later 
	if player:FindFirstChild("King") then -- removing the string values.
			player.King:Destroy()
		end

I don’t see any errors and the output too isn’t showing any errors.

What is possibly be wrong here?

1 Like

the script is looping through each player, each time it picks a “king”, then adds a king tag to that king, then checks if the “player” has the king tag.

if you remove the for loop and the if statement, it should work.

p.s. if player is Local Player, that can only be called in a local script, but what this script is doing, it should be handled by a server script.

1 Like

Thanks lemme check if it works.

Hey it works thanks but still it inserts king tag to every player in the match
How can I fix this is there a problem with my math.random?

Do you want the whole script or the same part?

just the part that adds the king tag.

1 Like

local king = plrs[math.random(1, #plrs)] 

local KingTag = Instance.new("StringValue")
KingTag.Parent = king
KingTag.Name = "King"

local KingTools = ServerStorage:WaitForChild("KingsTools"):GetChildren()

local RadWeapon = KingTools[math.random(1, #KingTools)]

local ClonedWeapon = RadWeapon:Clone()
ClonedWeapon.Parent = king.Backpack

if this part is only run once, it will only select one king, and add one king tag.
you could try looping through each player and if they have a king tag, destroy it before selecting a new king.
But make sure the king selection is only run once.

(sorry about the delay, I was gonna add a code block, but forgot how to format it.)

1 Like

Sorry man but the game chooses two players rather than one player.
If you want to know what is #plrs it is a table where I insert players into the table.

Sometimes it inserts two king tags to one player.

Since “king” is the player who has the tag, why don’t you just get the string value from that king and destroy that tag. Also how did you insert the players into a table?

I will send the whole script then you might understand.
Should I send the whole thing?

Ok, just point out parts that are relevant.