Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)

1
try using else on the if script.parent.transparency == 0? Pidgeyottoe 1 — 4y
0
still doesn't work TheKakYTArmy 125 — 4y
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. RazzyPlayz 497 — 4y
0
yeah I guess that's the only option TheKakYTArmy 125 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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)

0
I did something else but your script is the best here so I'll accept it TheKakYTArmy 125 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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
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. WizyTheNinja 834 — 4y

Answer this question