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
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
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
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)