Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make lazers not kill you when it disappears?

I tried asking this yesterday but I didn't get a good response so I'm asking it again.

Here is my script:

function onTouch(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if script.Parent.Transparency == 0 then
		if (humanoid ~= nil) then
			humanoid.Health = 0
		end
	end
end

while true do
	script.Parent.Transparency = script.Parent.Transparency + 0.5
	wait(0.5)
	script.Parent.Transparency = script.Parent.Transparency + 0.5
	wait(1)
	script.Parent.Transparency = 0
	wait(1.5)
end

script.Parent.Touched:Connect(onTouch)

4 comments

1

try using else on the if script.parent.transparency == 0?

0

still doesn't work

0

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.

0

yeah I guess that's the only option

2 answers

✓ 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 -- Adds .5 every second, so if you want to wait 1 second wait till .5, 2 seconds woulkd be waittime >= 1. 1.5 seconds would be waittime >= .75
				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)

1 comment

0

I did something else but your script is the best here so I'll accept it

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then	-- if a humanoid exists, then
		humanoid.Health = 0	-- damage the humanoid
	end 
end
function onTouch(part)
local humanoid = part.Parent:FindFirstChild("Humanoid") 
script.Parent.Touched:connect(onTouch)
if script.Parent.Transparency == 1 then
	if (humanoid ~= nil) then
		humanoid.Health = 100
	end
end	
end

1 comment

0

If you are going to answer a question, provide a little information behind it, and use the proper code formatting. Code doesn't go inside a code block, it goes inside the Lua button.

This discussion is preserved as it was. New answers, comments, and votes are closed.