✓ Accepted answer
Make sure you are using a server script and not a local script.
You cannot run a loop and wait for another part of code to run at the same time. The loop will continue to run and wont be able to check to see if it was touched.
Use a stepped function instead, using increments per second as a basis to fade in and out.
function onTouch(part)
print(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid == nil then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if script.Parent.Transparency <= 0 then
if (humanoid ~= nil) then
humanoid:TakeDamage(100)
end
end
end
local RunService = game:GetService("RunService")
local RATE_PER_SECOND = .5
local fadein = false
local waittime = 0
RunService.Stepped:Connect(function(time, step)
local increment = RATE_PER_SECOND * step
if fadein == false then
script.Parent.Transparency = script.Parent.Transparency + increment
if script.Parent.Transparency >= 1 then
waittime = waittime + increment
if waittime >= .5 then
fadein = true
waittime = 0
end
end
else
script.Parent.Transparency = script.Parent.Transparency - increment
if script.Parent.Transparency <= 0 then
waittime = waittime + increment
if waittime >= .5 then
fadein = false
waittime = 0
end
end
end
end)
script.Parent.Touched:Connect(onTouch)
4 comments
try using else on the if script.parent.transparency == 0?
still doesn't work
The reason is because you are only changing the transparency of the laser. This will only make it invisible, however, it will still be there. Either change the position of the laser or remove it when it's supposed to be invisible.
yeah I guess that's the only option