It also fires many times when I touch it once and when I stop touching it.
This is my attempt to debounce but it doesnt seem to work. I’ve tried everything I could think of, can anyone help me on this?
model = script.Parent
button = script.Parent.Parent.BlockSpawner.Button
local canTouch = true
local function changeColor()
if canTouch == true then
print("Through")
canTouch = false
for _, block in ipairs(model:GetChildren()) do
if block.Name == "Part" then
local randomNumber = math.random()
if randomNumber < 0.80 then
block.BrickColor = BrickColor.new("Bright red")
else
block.BrickColor = BrickColor.new("Bright green")
end
end
end
end
end
local function touchEnded()
print("Ended")
canTouch = true
end
local function taskSpawn()
changeColor()
end
button.Touched:Connect(taskSpawn)
button.TouchEnded:Connect(touchEnded)
Roblox is weird and only registers touches on the corners of the parts, meaning that every time you move to the middle of the part it thinks you stopped touching it. A solution you could probably take here is to add some sort of cooldown, making your code look something like this:
local function changeColor()
if canTouch == true and not on_cooldown then
print("Through")
on_cooldown = true
canTouch = false
for _, block in ipairs(model:GetChildren()) do
if block.Name == "Part" then
local randomNumber = math.random()
if randomNumber < 0.80 then
block.BrickColor = BrickColor.new("Bright red")
else
block.BrickColor = BrickColor.new("Bright green")
end
end
end
wait(0.5)
on_cooldown = false
end
end
There are multiple ways to solve this, and personally I’d just use GetPartsInParts or the Zones module to do this, but this is probably the easiest way to solve it given your code.
Yea I’ll try using GetPartsInParts it seems like a better idea, I’m guessing I’d have to basically constantly check if a user’s feet touched the button and if they did, wait until no parts are touching the button until it can detect again if a user’s feet is touching the button. And by constantly I mean all this in a while true.