So I have a button that I want to change the item in the players' backpacks from one sword to another. I have it set to be togglable so that every click switches the sword from one to the other, and back again, etc. When I click the button the first time, it works fine. But when I click it for the second time, it fails to run and gives me this error:
LinkedSword is not a valid member of Backpack
Script 'Workspace.Button.Script', Line 8
Stack End
Disconnected event because of exception
I have done some looking on the wiki and the closest thing to help with toggle setups is under the "Not" Operator on the Boolean page. I don't know why, but it seems to be ignoring the "if" statement and jumping right into running the first set of code. By the way, don't worry about the large sections of code, it all works fine. I asked a question similar to this and the guy who answered told me a better way to code that part but didn't actually help me solve the problem I was having.
Button = script.Parent
toggle = false
Button.ClickDetector.MouseClick:connect(function()
toggle = not toggle
if toggle then
for i, player in ipairs(game.Players:GetPlayers()) do
if player.TeamColor == ("Navy blue") or ("Bright red") then
player.Backpack.LinkedSword:remove()
game.Teams.Apostles.LinkedSword:remove()
game.Teams.Raiders.LinkedSword:remove()
local ClonedSword = game.ServerStorage.TeamedSword:Clone()
ClonedSword.Parent = player.Backpack
local ClonedSword2 = game.ServerStorage.TeamedSword:Clone()
ClonedSword2.Parent = game.Teams.Apostles
local ClonedSword3 = game.ServerStorage.TeamedSword:Clone()
ClonedSword3.Parent = game.Teams.Raiders
Button.BrickColor = BrickColor.Red()
else
for i, player in ipairs(game.Players:GetPlayers()) do
if player.TeamColor == ("Navy blue") or ("Bright red") then
player.Backpack.TeamedSword:remove()
game.Teams.Apostles.TeamedSword:remove()
game.Teams.Raiders.TeamedSword:remove()
local ClonedSword = game.ServerStorage.LinkedSword:Clone()
ClonedSword.Parent = player.Backpack
local ClonedSword2 = game.ServerStorage.LinkedSword:Clone()
ClonedSword2.Parent = game.Teams.Apostles
local ClonedSword3 = game.ServerStorage.LinkedSword:Clone()
ClonedSword3.Parent = game.Teams.Raiders
Button.BrickColor = BrickColor.Green()
end end end end end end)

1 comment
Yeah, some help doing that would be nice.