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

Is it possible for conflicting conditions to be true at the same time?

Asked by
vinboi 0
4 years ago

In the output, things work as expected with red alternating being printed with blue, but in game, even with both conditions being true my part remains blue. I expect blue and red to alternate like in the output. I don't understand why information from output doesn't match up with the color of the brick.

local Point_A = game.Workspace.Points.Point_A

while true do
    wait(1)
    for i, v in pairs(game.Players:GetChildren()) do
        if v.Character then
            if (v.Character.Torso.Position - Point_A.Position).magnitude < 15 and v.TeamColor == BrickColor.new("Really blue") then
                Point_A.BrickColor = BrickColor.new("Really blue")
                print("Set to blue")


            end


        end
        if v.Character then
            if (v.Character.Torso.Position - Point_A.Position).magnitude < 15 and v.TeamColor == BrickColor.new("Really red") then
                Point_A.BrickColor = BrickColor.new("Really red")
                print("Set to red")
            end
        end

    end
end

2 answers

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

Why the first if statement and the line 05 have this line:

for i, v in pairs(game.Players:GetChildren()) do

But the next if statement doesn't have this?

Maybe put the next if statement this line I guess:

for i, v in pairs(game.Players:GetChildren()) do
0
sorry my gramar suck Xapelize 2658 — 4y
0
cus its encapsulated in the first statement TheluaBanana 946 — 4y
0
but his format very wierd TheluaBanana 946 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The outcome will always be blue, as you may have coincidentally checked for blue last(in your table of players, the last player that the script checked for is probably on the blue team). In the event in which red and blue are both contesting the area, and both conditions are true, the logic of the script would be to first set the brickcolor to red, and in the next instance, switch it to blue. So it actually does set the brickcolor to red, but just quickly changes it to blue afterwards. This would also explain the output. The checks for red and blue are not run simultaneously; they just have a extremely short interval between them. To get around this, one solution off the top of my head would be to just write a chunk of code to check for all three possibilities(if blue only, if red only and if both blue and red):

local pointA = game.Workspace.points.Point_A

while wait(1) do
    local blue = false
    local red = false

    for i, v in pairs(game.Players:GetChildren()) do
        if v.Character then
            local dist = (v.Character.Torso.Position - pointA.Position).magnitude

            if dist <= 15 and v.TeamColor == BrickColor.new("Really blue") then
                blue = true
            elseif dist <= 15 and v.TeamColor == BrickColor.new("Really red") then
                red = true
            end
        end
    end

    if red and not blue then
        pointA.BrickColor = BrickColor.new("Really red")
    elseif blue and not red then
        pointA.BrickColor = BrickColor.new("Really blue")
    elseif blue and red then
        if pointA.BrickColor == BrickColor.new("Really red") then
            pointA.BrickColor = BrickColor.new("Really blue")
        elseif pointA.BrickColor == BrickColor.new("Really blue") then
            pointA.BrickColor = BrickColor.new("Really red")
        end
    end
end

Answer this question