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

Can I make this script stop killing said player at certain area?

so i have this slow killing script, I want it to stop damaging the player when the player steps out of the "gas" area can anyone help?

function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid") 
if h~=nil then 
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -1
wait (1.5)
h.Health = h.Health -100
wait (1.5)
end 
end
script.Parent.Touched:connect(onTouched)

3 comments

0

Could you please put your script in a code block so we can read and understand your code much easier?

0

oh yah

0

Why dont you just add a while loop and then put line 3 and 4 once, saves tons of space.

2 answers

✓ Accepted answer

###First off, wow. That is one hell of a monstrosity. You have never heard of for loops, these are your friends in code shortening.

local touching = false -- checks if touching 

local function onTouch(part)
    local hum = part.Parent:FindFirstChild("Humanoid") -- uppercase F, lowercase f is deprecated
    
    if hum then -- no need for ~= nil
        touching = true -- it's a humanoid, has to be a player

        for i = hum.Health, 0, -1 do -- decrease health by 1 each increment
            if touching then -- make sure they're touching the gas!!
                hum.Health = i
                wait(1.5)
            else -- player isn't touching the gas...
                break -- ... we're going to end the loop since they didn't touch!
            end
        end
    end
end

script.Parent.Touched:Connect(onTouch) -- Use Connect instead of connect, the latter is deprecated and should not be used in new work

script.Parent.TouchEnded:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        touching = false -- set to false on touch end
    end
end)

4 comments

0

ok i tried this one too, my player isnt taking damage do i need to rename something or anything i need to change for this script to work?

0

Ah i see what i did

0

The third number should have been -1

0

thanks bro

Instead of only damaging the player while they are inside the area, you are checking to see if they step into the area then damaging them until they die. You should reconsider how you are going about this, and think about using a while loop. The while loop will check for a condition and see if it is true before looping again.

Here is an idea of what you should do:

local playerIsInGas = false

gas.Touched:Connect(function()
	playerIsInGas = true
	while playerIsInGas then
		playerHumanoid:TakeDamage(1.5)
		wait(1.5)
	end
end)

gas.TouchEnded:Connect(function()
	playerIsInGas = false
end)

Also, please use code blocks (the blue Lua button) to format your code.

Hope this helps! :)

2 comments

0

that seems right i tried it out and my player isnt taking damage, do i need to change anything? with the brick or the script?

0

Yes, you need to change the script to match all the variables. The script I gave isn't your answer to just copy and paste, but an idea as to what you should do

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