In my game, when a player steps onto a brick, the brick is supposed to fade away and once completely transparent, turn CanCollide to false. I tried this script, but it did not do anything when the player steps on the brick.
local trans = script.Parent.Transparency function onTouch(hit) while trans < 1 do trans = 0.1 wait(0.1) trans = trans + 0.1 if trans == 1 then script.Parent.CanCollide = false end wait(3) trans = 0 script.Parent.CanCollide = true end end script.Parent.Touched:connect(onTouch)
Please help.
Your < is lonely on line 3, as transparency is capped at 1, using <= is probably what you want to do.
Additionally, remove the conditional on line 08, and move lines 9 and 11-13 to after line 14. Once the while loop breaks it's already guaranteed to be completely transparent.
Lastly trans is interpreted as a value, not an object, you'll need to use something like part = script.Parent // part.Transparency = part.Transparency + 0.1
instead of using trans = trans + 0.1, that goes for the while condition as well.
I tried working with your code but I kept encountering odd behaviour, so I optimized it a bit. The code below is there if needed, but I recommend not using it since it doesn't exactly help
you per say (Its more of my own created script than an edit of yours).
Either way, its up to you.
local enabled = true --a debounce variable local part = script.Parent -- This is the part we're dealing with function onTouched() if not enabled then return end --If enabled==false, stop the function. It is currently operating transparency changes enabled = false --Make enabled = false so we won't encounter any issues when the part is touched multiple times for i = 1, 10 do --while loops are recommended for these type of problems. This loop will run 10 times. i = the amount of times the loop has ran part.Transparency = i/10 --Amount of time the loop ran divided by 10 gives a 0.1 decrease per run. wait(1)--Wait one second before repeating the code. end part.CanCollide = false --The loop is now done, and the tranparency should be 10/10 (1). We set the parts cancollide to false wait(3)--Wait three seconds part.CanCollide = true --Make the part CanCollide=true again part.Transparency = 0 --Make the part visible again enabled = true --Reenable it. end script.Parent.Touched:Connect(onTouched) -- Connection line