[Making a “reactor core” game for context]
I am on the last hurdle of this coolant system…
I made it to where the button (clickdetector) will have a cooldown after it’s turned on, but for reasons my non-scripter brain cannot recognize, players can immediately turn the system back off without waiting.
Here is my current script—the cooldown is still functional, but only for setting the coolant back on. Which gives a clear advantage for the people who want to see my fine work go BOOM.
local clicked = false
local function I ()
local temp = game.Workspace.Values["THE HEAT"]
if clicked == false then
clicked = true
script.Parent.BrickColor = BrickColor.new("Rust")
repeat
wait(2)
temp.Value += math.random(-3,-1)
task.wait(math.random(2,4))
until clicked == false
script.Parent.ClickDetector.MaxActivationDistance = 0
wait(5)
script.Parent.ClickDetector.MaxActivationDistance = 10
else
clicked = false
script.Parent.BrickColor = BrickColor.new("Cocoa")
script.Parent.ClickDetector.MaxActivationDistance = 0
wait(5)
script.Parent.ClickDetector.MaxActivationDistance = 10
end
end
script.Parent.ClickDetector.MouseClick:Connect(I)
Yes, this is based off of a YouTube tutorial. I’ve been staring at this code forever, I’ve resorted it many times, I’m at a loss for what to do. Please help…?
-- save the 2 colors into variables
local color1 = BrickColor.new("Rust")
local color2 = BrickColor.new("Cocoa")
-- when the click detector is clicked call this function
script.Parent.ClickDetector.MouseClick:Connect(function()
-- set the max distance to 0 so they cant click again
script.Parent.ClickDetector.MaxActivationDistance = 0
-- flip the colors around
if script.Parent.BrickColor == color1 then
script.Parent.BrickColor = color2
else
script.Parent.BrickColor = color1
end
-- wait 2 seconds
task.wait(2)
-- set the max distance to 10 so the player can click again
script.Parent.ClickDetector.MaxActivationDistance = 10
end)
This script for the cooldown works great! However…
It does eliminate the main purpose of my original code. Would you know how to merge the two scripts so that the temperature function still works with your cooldown?
Sorry about this. It’s not well hidden that I’m a beginner with programming.
-- get a reference to the THE HEAT value
local heat = game.Workspace.Values["THE HEAT"]
-- save the 2 colors into variables
local color1 = BrickColor.new("Rust")
local color2 = BrickColor.new("Cocoa")
-- a id number will tell us what loop we are on
local id = 1
local function Loop()
-- make a local variable that will save the current id
local currentId = id
-- while the current id is equal to the id keep looping
while currentId == id do
-- reduce the heat by 1, 2 or 3
heat.Value -= math.random(1, 3)
-- wait for 2, 3 or 4 seconds
task.wait(math.random(2, 4))
end
end
-- when the click detector is clicked call this function
script.Parent.ClickDetector.MouseClick:Connect(function()
-- set the max distance to 0 so they cant click again
script.Parent.ClickDetector.MaxActivationDistance = 0
-- flip the colors around
if script.Parent.BrickColor == color1 then
script.Parent.BrickColor = color2
-- spawn the loop function that will loop over and over as long as the id does not change
task.defer(Loop)
else
script.Parent.BrickColor = color1
-- add 1 to the id to stop the loop
id += 1
end
-- wait 10 seconds
task.wait(10)
-- set the max distance to 10 so the player can click again
script.Parent.ClickDetector.MaxActivationDistance = 10
end)
Thank you! This works perfectly. I’ve compared your script to my old one, I’m recognizing all the differences.
Lua isn’t as straightforward as I thought. This really helps me understand more as a newbie to programming.