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

Why doesn't this GUI script work?

Asked by 10 years ago

I have two problems; A) Roblox Studio crashes when I try to test the game (It works if I remove the script that is at the bottom) and B) When a player captures the flag in the middle of my game, I want the GUI to start adding points for what ever team captured it, which is does not currently.

Heres the code;

flag = game.Workspace.Flag.Flag
B = script.Parent.Blue
R = script.Parent.Red

while true do
    if flag.BrickColor == "Bright blue" then
        wait(1)
        B.Text = "Blue:"..1+B.Text
    if flag.BrickColor == "Bright red" then 
        wait(1)
        R.Text = "Red:"..1+B.Text
    end
    end     
end

B is the GUI responsible for the amount of points blue team has and R is for the amount of points for the red team.

Please help!

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

1.) You need a wait in the while loop, because if both aren't true those waits won't do anything.

2.) You can't add numbers and strings, just like you can't add a+1. However, if the string is a number you can convert it so you can add it. - tonumber(b.Text) + 1

3.) You need an elseif, because the second if isn't going to be checked if the first one isn't true.

flag = game.Workspace.Flag.Flag
B = script.Parent.Blue
R = script.Parent.Red

-- Your text isn't solely a number, so you need to separate the text from the number. 
local function find_num(text)
    for i in string.gmatch(text, "%d+") do 
        return tonumber(i)
    end
end

while true do
    if flag.BrickColor == "Bright blue" then
        B.Text = "Blue:"..find_num(B.Text) + 1
    elseif flag.BrickColor == "Bright red" then 
        R.Text = "Red:"..find_num(B.Text)  + 1
    end     
    wait(1)
end

Ad

Answer this question