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

Scope always goes to the last if statement?

Asked by 4 years ago

In the script below it always chooses the last if statement. The values are intvalues that count up the points. And if a team wins it will show the points comparing but if the teams tie it will show it was a tie. But for some reason it always says its a tie even tho a value is greater than the other. Can anyone help? Sorry if you didn't understand.

local GameLength = workspace.MapChosen:FindFirstChildWhichIsA("Folder").Info.GameLength.Value
for i = GameLength,1,-1 do 
    Status.Value = "Destroy the other teams statue before the other team does!".." (" ..i..")"
    for x, player in pairs(plrs) do
        if player then
            local character = player.Character
            if not character then
                table.remove(plrs,x)
            end
        wait(1)
    end

        if i == 1 then
            local BlueTeamStatue = script.Parent.RedTeamStatue:FindFirstChild("Tracker")
            local RedTeamStatue = script.Parent.RedTeamStatue:FindFirstChild("Tracker")
            if RedTeamStatue.Value.Value < BlueTeamStatue.Value.Value then
                Status.Value = "Red team wins! with a score of ".. tostring(RedTeamStatue.Value.Value).. " to " .. tostring(BlueTeamStatue.Value.Value)
                ValueRemover()
                game.Teams.Red:Destroy()
                game.Teams.Blue:Destroy()
                wait(5)
                break
        elseif BlueTeamStatue.Value.Value < RedTeamStatue.Value.Value then
                Status.Value = "Blue team wins! with a score of ".. tostring(BlueTeamStatue.Value.Value).. " to " .. tostring(RedTeamStatue.Value.Value)    
                ValueRemover()
                game.Teams.Red:Destroy()
                game.Teams.Blue:Destroy()
                wait(5)
                break
            elseif BlueTeamStatue.Value.Value == RedTeamStatue.Value.Value then
                Status.Value = "It was a tie!"
                ValueRemover()
                game.Teams.Red:Destroy()
                game.Teams.Blue:Destroy()
                wait(5)
            end
        end
    end
end

0
It simply means that BlueTeamStatue.Value.Value = RedTeamStatue.Value.Value. There is nothing we can do unless if you give us the code that decides these values. brokenVectors 525 — 4y
0
What does ValueRemover do? gskw 1046 — 4y
0
0
@gskw they remove values out of the character but you can ignore them I guess they really have nothing to do with this. GamerLoIiaz_v2 -4 — 4y
View all comments (2 more)
0
Is there something that would reset Tracker.Value.Value? gskw 1046 — 4y
0
No nothing should reset the value... GamerLoIiaz_v2 -4 — 4y

1 answer

Log in to vote
0
Answered by
gskw 1046 Moderation Voter
4 years ago
Edited 4 years ago

You have a minor typo which is causing you to compare the red team's score with itself. Additionally, you have a bug in the logic, which is obscured by improper indentation. Let's format your code correctly:

local GameLength = workspace.MapChosen:FindFirstChildWhichIsA("Folder").Info.GameLength.Value
for i = GameLength, 1, -1 do
    Status.Value = "Destroy the other teams statue before the other team does!" .. " (" .. i .. ")"
    for x, player in pairs(plrs) do
        if player then
            local character = player.Character
            if not character then
                table.remove(plrs, x)
            end
            wait(1)
        end

        -- !!! The following code runs several times when i reaches 1, once for each player.
        -- You probably want to move this outside the for loop.
        if i == 1 then
            local BlueTeamStatue = script.Parent.BlueTeamStatue:FindFirstChild("Tracker") -- Here, I changed script.Parent.RedTeamStatue to script.Parent.BlueTeamState
            local RedTeamStatue = script.Parent.RedTeamStatue:FindFirstChild("Tracker")
            if RedTeamStatue.Value.Value < BlueTeamStatue.Value.Value then
                Status.Value =
                    "Red team wins! with a score of " ..
                    tostring(RedTeamStatue.Value.Value) .. " to " .. tostring(BlueTeamStatue.Value.Value)
                ValueRemover()
                game.Teams.Red:Destroy()
                game.Teams.Blue:Destroy()
                wait(5)
                break
            elseif BlueTeamStatue.Value.Value < RedTeamStatue.Value.Value then
                Status.Value =
                    "Blue team wins! with a score of " ..
                    tostring(BlueTeamStatue.Value.Value) .. " to " .. tostring(RedTeamStatue.Value.Value)
                ValueRemover()
                game.Teams.Red:Destroy()
                game.Teams.Blue:Destroy()
                wait(5)
                break
            elseif BlueTeamStatue.Value.Value == RedTeamStatue.Value.Value then
                Status.Value = "It was a tie!"
                ValueRemover()
                game.Teams.Red:Destroy()
                game.Teams.Blue:Destroy()
                wait(5)
            end
        end
    end
end

0
Thank you! I will upvote and accept. And I was so stupid to not even realize that. GamerLoIiaz_v2 -4 — 4y
Ad

Answer this question