Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this toggle script not working?

Asked by 10 years ago

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 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

Well, that's because the two swords are named differently. There are a few solutions for this. One, you could change "TeamedSword" to "LinkedSword". The other, you could add another if statement to check which sword is in their Backpack. Also, the sword is not limited to be in the Backpack. A Player COULD click the button with the sword in their hand, which would be in their character. I recommend adding an if statement to check if either of the two tools are in the Player's character. Hope this helped. If you need me to show you where to edit, feel free to comment.

0
Yeah, some help doing that would be nice. orderman 15 — 10y
Ad

Answer this question