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

How to change a variable inside a function to activate a while true do loop?

Asked by
pilotly 15
3 years ago

Hello! I have made few posts on this but here are the basics of this issue I'm trying to learn from.

Basically I'm trying to get a boolean variable to turn true so that a while true loop can activate.

It would help me greatly to tell me what the mistake is so I won't do it again. :)

Trouble code:

local function buttonPressed(Messed)
    local character = Messed.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    --Functions cant be called in if statements
    if humanoid and sentinal2 == true then      
        activetime = true
    end
end     



finishline.Touched:Connect(partTouched)
button.Touched:Connect(buttonPressed)

while activetime == true do
        print("Debug3") 
    wait(1) timepassed = timepassed + 1
        print(timepassed)
end

Full code:

local finishline = script.Parent
local timepassed = 0
local sentinal = true

local button = workspace.startr
local sentinal2 = true

local activetime = false

local function finish()
    sentinal = false
    print("you finished in" .. timepassed)
    if timepassed <= 10 then

        print("gold") 
    elseif timepassed > 10 and timepassed <= 20 then
        print("silver")

    elseif timepassed > 20 and timepassed <= 30 then 
        print("bronze")
    else
    print("try agian")  
    end
end

local function partTouched(Touched)

    local character = Touched.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid and sentinal == true then       

    end
end


local function buttonPressed(Messed)
    local character = Messed.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    --Functions cant be called in if statements
    if humanoid and sentinal2 == true then      
        activetime = true
    end
end     



finishline.Touched:Connect(partTouched)
button.Touched:Connect(buttonPressed)

while activetime == true do
        print("Debug3") 
    wait(1) timepassed = timepassed + 1
        print(timepassed)
end
0
It's not a very good setup, but you could have an if statement wrapped around everything inside of your while loop. Then it will only run your code when the boolean is true.. Nickuhhhhhhhhhhhhhhh 834 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
  • You can use BindableEvents to wait until something happens or tables to store information about all players
  • Functions can be called in if statements
  • (Style) You have inconsistent capitalization; in Roblox, camelCase is common but you have things like timepassed (which is harder to read)
local Players = game.Players
local finishLine = script.Parent
local startButton = workspace.startr

local playerToStartTime = {} -- we'll use this to keep track of when a player hit the start button last

local function finish(player, timePassed)
    print(player, "finished in", timePassed)
    if timePassed <= 10 then
        print("gold")
    elseif timePassed <= 20 then -- note: no need to check if "timePassed > 10" since you already checked to see if it's <= 10
        print("silver")
    elseif timePassed <= 30 then 
        print("bronze")
    else
        print("try again")
    end
end

startButton.Touched:Connect(function(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if player then
        playerToStartTime[player] = os.clock() -- record that this player has started the race at this time
    end
end)
finishLine.Touched:Connect(function(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)
    local startTime = playerToStartTime[player] -- it's okay if 'player' is nil
    if startTime then
        playerToStartTime[player] = nil
        finish(player, os.clock() - startTime) -- call 'finish' with the results for this player
    end
end)
Players.PlayerRemoving:Connect(function(player)
    playerToStartTime[player] = nil -- ensure we don't leak memory if the player leaves without finishing the race
end)
Ad
Log in to vote
1
Answered by 3 years ago

The reason why this doesn't work is because the while loop isn't constantly being checked to see if the condition is true. While loops only run if the condition is met. Just like an if statement. The only difference being that it is repeating the code on a condition and not once (if statement). Because the while loop's condition is not met, it will not run and will be ignored for the script's lifetime. If you want the while loop to be checked again, try using a function.

Answer this question