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

How would I properly execute a function if a bool is true?

Asked by 9 years ago

I have a bool called Driverswin in the workspace. How would I make a script that when that bool is true it does a fuction called like Message or something?

1 answer

Log in to vote
2
Answered by
4Bros 550 Moderation Voter
9 years ago

I'm assuming you want that function to fire everytime that bool is changed to true, rather than checking if it's true just one time. To do this, you need to use the bool's Changed event, it will return a parameter of the boolvalue's value which can be either true or false.

01local workspace = game:GetService'Workspace'
02local bool = workspace:WaitForChild'BoolValue' -- Change what is inside the apostraphes to the boolvalue's name
03 
04function Message()
05    local message = Instance.new("Hint",workspace)
06    message.Text = "This is a message"
07    game:GetService'Debris':AddItem(message,4)
08end
09 
10 
11bool.Changed:connect(function(value) -- This will fire every time the boolvalue is changed
12    if value then --If bool value is true
13        Message() -- Call message
14    end
15end)
Ad

Answer this question