Problem with Part Transparency script

The problem is that I’m trying to make CanCollide true on a part when it’s transparency reaches 0 but for some reason the script is returning a weird number value that I never intended to reach and because of that I can’t make it turn CanCollide on, here’s the script that I’m using:

Script
local part = script.Parent
local TouchedCooldown = false

part.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	
	if Humanoid and not TouchedCooldown then
		TouchedCooldown = true
		
		for i = 0,1,0.1 do
			part.Transparency = i
			wait()
		end
		
		print(part.Transparency)
		
		if part.Transparency == 1 then
			part.CanCollide = false
			print("can collide is false")
		end
		print("moving on")
		
		wait(0.5)
		
		for i = 1,0,-0.1 do
			part.Transparency = i
			wait()
		end
		
		print(part.Transparency)
		
		if part.Transparency == 0 then
			part.CanCollide = true
			print("cancollide is on")
		end
		
		print("yes")
		
		wait(0.5)
		TouchedCooldown = false
	end
end)

Because of the debugging that I did I was able to see the reason as to why it’s not turning CanCollide on and it’s because of the weird number value which I never intended to reach like I said above, here’s a picture of my output so you can see what prints in the script, what doesn’t and what’s happening:

NumberIssue

Now as you can see when I printed the part.Transparency number value after it’s 0 for some reason it’s not printing 0 instead it prints this weird HUGE number value that I never intended to reach and the Part’s Transparency is 0 after the numeric loop is done and not the weird number value so what’s happening here and how do I fix this?

Is this the number floating point error issue within Roblox Lua or something?

It is an issue with Lua, and has been reported several times. For now just do division instead:

You also don’t need to check if the transparency is 1 or if it’s 0. Just run the code at the end of the loop.

local part = script.Parent
local TouchedCooldown = false

part.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	
	if Humanoid and not TouchedCooldown then
		TouchedCooldown = true
		
		for i = 0, 10 do
			part.Transparency = i/10
			wait()
		end
		
		print(part.Transparency)
		part.CanCollide = false
		
		print("can collide is false, moving on")
		
		wait(0.5)
		
		for i = 10, 0, -1 do
			part.Transparency = i/10
			wait()
		end
		
		print(part.Transparency)
		part.CanCollide = true
		print("cancollide is on, yes")
		
		wait(0.5)
		TouchedCooldown = false
	end
end)
1 Like