Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Scope always goes to the last if statement?

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

6 comments

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.

0

What does ValueRemover do?

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.

0

Is there something that would reset Tracker.Value.Value?

0

No nothing should reset the value...

1 answer

✓ Accepted answer

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

1 comment

0

Thank you! I will upvote and accept. And I was so stupid to not even realize that.

This discussion is preserved as it was. New answers, comments, and votes are closed.