Issue with repeating if statement

Hi, I have the following code :

while wait() do
	
	if player.Team == teams["King"] then 
		pieceStatus.Visible = true
		if workspace:FindFirstChild("Map") and workspace.Map:FindFirstChild("Palace") then	    
			for i, descendant in pairs(workspace.Map.Palace:GetDescendants()) do

				if descendant:IsA("Tool") and descendant:FindFirstChild("Handle") and not game.Players:GetPlayerFromCharacter(descendant.Parent) then
						
					if descendant.Handle:FindFirstChild("TouchInterest") then descendant.Handle.TouchInterest:Destroy() end
						
					descendant.Handle.ProximityPrompt.Enabled = true
					
					
				end
			end
		end
		
	else
	pieceStatus.Visible = false
	
	end

	if player.Team == teams["King"] then
		KingPieces.Value ="0/3"
		tools = {}
		for _, tool in pairs(player.Backpack:GetChildren()) do
			table.insert(tools, tool)
			if #tools == 1 then
				KingPieces.Value = "1/3"
			elseif #tools == 2 then
				KingPieces.Value = "2/3"
			elseif #tools == 3 then
				KingPieces.Value = "3/3"
				local Event = game.ReplicatedStorage:WaitForChild("GoldenSword")
				

				Event:FireServer("GoldenSword")
				script.Parent.Announcement.Text = "Sword Given!"
				script.Parent.Announcement.Visible = true
				wait(5)
				script.Parent.Announcement.Visible = false

		end
	end
end
end

So in the elseif statement for tools = 3, the Event fires as expected (and the player gets a sword) making the total number of tools in the players backpack equal to 4. So why then does this last elseif statement keep on repeating and repeating (after every wait(5)) even though there isnt only 3 tools in the players backpack (but 4 now as they have the first 3 + this new sword from the event i fired) giving them an infinite number of swords
Hope that makes sense
Thanks

Hi! Do you have the script containing the elseif statement?

What do you mean, the script for ‘Event’ ?

Yes, the script where you’re using the elseif statement.

I think it’s because of the "for_, " it keeps saying you have 3 tools instead of upping it to four tools.

		elseif #tools == 3 then
			KingPieces.Value = "3/3"
			local Event = game.ReplicatedStorage:WaitForChild("GoldenSword")

I think you forgot to add the newly added tool to the tools table

image

Sword given, but the tools table is not updated

Put this once the sword is given to the player

table.insert(tools, wherever the newly cloned tool is)

Thanks! For some reasons I couldn’t scroll down.

So the new tool will be placed in the backpack with the rest of them , how Would i add just that one without adding the rest again? (Other than just removing them all and then adding them all again)? Thanks everyone for replies too btw

If you mean adding the new tool to the tools table, then you just do

local newsword = player.Backpack:WaitForChild("GoldenSword")
table.insert(tools, newsword)

Orr a better alternative without using tables:

local toolCount = #player.Backpack:GetChildren()

The thing I dont understand is the rest of them seem to add fine with using this (like as soon as the second one is picked up it will register)

for _, tool in pairs(player.Backpack:GetChildren()) do

So im not sure why it wouldn’t still work when the sword is added in as the 4th tool?

Most likely the value you’re inserting is nil
wait no
since you’re iterating through every tool in your backpack,
#tools will always be 1, 2, 3,… (until the amount of tool in the backpack) and so on which might not be what you want

I suggest just replacing


#tools 

to

toolCount = #player.Backpack:GetChildren()
if player.Team == teams["King"] then
    local toolCount = #player.Backpack:GetChildren()
    if toolCount == 1 then __ end
    if toolCount == 2 then __ end
    if toolCount == 3 then __ end

Ok thanks for that, so I changed the code to this:

	if player.Team == teams["King"] then
		pieceStatus.Visible = true
	
		KingPieces.Value ="0/3"
		local toolCount = #player.Backpack:GetChildren()
			
		if toolCount == 1 then KingPieces.Value = "1/3" end
		if toolCount == 2 then KingPieces.Value = "2/3" end
		if toolCount == 3 then KingPieces.Value = "3/3" 
			local Event = game.ReplicatedStorage:WaitForChild("GoldenSword")
			Event:FireServer("GoldenSword")
			script.Parent.Announcement.Text = "The King has Crafted his Sword!"
			script.Parent.Announcement.Visible = true
			wait(5)
			script.Parent.Announcement.Visible = false
		end
		if toolCount == 4 then KingPieces.Value = "3/3" end
	else
		pieceStatus.Visible = false
end

And there is a slight issue where, when the player gets the sword (4 tools in total) when he goes to equip the sword, and when you equip a tool it gets removed temporarily from the backpack. So when the player equips it the script detects that the tool number has went back from 4 to 3, so the first time he ever equips it, a second sword spawns (let me know if that doesnt make sense)

I know a workaround to this would be to remove the 3 pieces once he has the sword (which would be more ideal for the scenario too) But every time Ive tried that it bugs

Oh yea I forgot to take into account the tool that the player is equipping. To fix that, you simply do

toolCount = #player.Backpack:GetChildren()
toolCount = (player.Character:FindFirstChildOfClass("Tool") ~= nil and toolCount + 1) or toolCount

Basically what the script does is a simplified if__ then__ check.

if player.Character:FindFirstChildOfClass("Tool") then
    toolCount += 1
end
1 Like

That worked great, thank you very much!

Hey, I dont need to :Disconnect() my event at all when they get the sword do they (As its a round based game and when the round ends I believe they get all the tools removed from them anyway)

Yea you don’t need to disconnect at all