Help with my part event

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.

-- 
local part = workspace.Part

local function onPartTouched(part)
	wait(1)
	part.Transparency = 0.1
	wait(0.1)
	part.Transparency = 0.2
	wait(0.1)
	part.Transparency = 0.3
	wait(0.1)
	part.Transparency = 0.4
	wait(0.1)
	part.Transparency = 0.5
	wait(0.1)
	part.Transparency = 0.6
	wait(0.1)
	part.Transparency = 0.7
	wait(0.1)
	part.Transparency = 0.8
	wait(0.1)
	part.Transparency = 0.9
	wait(0.1)
	part.Transparency = 1.0
	
	part.CanCollide = false
	
	wait(1)
	part.Transparency = 1.0
	wait(0.1)
	part.Transparency = 0.9
	wait(0.1)
	part.Transparency = 0.8
	wait(0.1)
	part.Transparency = 0.7
	wait(0.1)
	part.Transparency = 0.6
	wait(0.1)
	part.Transparency = 0.5
	wait(0.1)
	part.Transparency = 0.4
	wait(0.1)
	part.Transparency = 0.3
	wait(0.1)
	part.Transparency = 0.2
	wait(0.1)
	part.Transparency = 0
	
	part.CanCollide = true
	
end

part.Touched:Connect(onPartTouched)

What seems to be wrong here?

1 Like

Woah, thats alot of code that can be shortened…

Anyway, you should probably remove the part argument in the function

local function onPartTouched() -- remove part
-- ...
end

and on the side note, you can reduce the long lines of code with loops:

for i = 1, 10 do
   part.Transparency += 0.1
end
for i = 1, 10 do
   part.Transparency -= 0.1
end

or TweenService:

local TweenService = game:GetService("TweenService")
-- ...
local fadeInTween = TweenService:Create(part, TweenInfo.new(1), {Transparency = 1})
local fadeOutTween = TweenService:Create(part, TweenInfo.new(1), {Transparency = 0})
local function onPartTouched()
   fadeInTween:Play()
   fadeInTween.Completed:Wait()
   part.CanCollide = false

   task.wait(1)
   fadeOutTween:Play()
   fadeOutTween.Completed:Wait()
   part.CanCollide = true
end

Always read the documentation if you need help!

TweenService
Loops
Functions and Events

1 Like

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.

Hope this helps!

This one works! Also thanks for telling me on how I can shorten my code, will remember for future scripts. Cheers!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.