I want to make this part turn invisible when touched, and then become visible again after a while where the function can be done again. The transparency of the part shifts from low to high then high to low.
The problem is that every time the part is touched, the function is repeated over and over. I only want the function to be executable when it’s visible.
I’ve tried changing onPartTouched(part) to onTouch(part) but it makes the player’s HumanoidRootPart visible which will cause some of their body parts to turn invisible then reappear again.
It sounds like your script is firing multiple times every time it gets touched. This is because a character is made up of many parts and each part that touches it will set your code off.
There are a couple of things you should do.
The most widely used technique is adding a debounce. For instance,
local debounce = false
local function onPartTouched()
if debounce == false then
debounce = true
-- code
part.CanCollide = true
debounce = false
end
I also recommend cleaning up your code, you can simply do something like @Korate168 said or
repeat
part.Transparency += 0.1
task.wait(0.1)
until part.Transparency == 1
That’s up to you though.
If you want, you should also add a check to make sure that the part that was touched belongs to a player.