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

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

Asked by 5 years ago
Edited 5 years ago

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)
0
Could you please put your script in a code block so we can read and understand your code much easier? lunatic5 409 — 5y
0
oh yah Cloudwolf39 -13 — 5y
0
Why dont you just add a while loop and then put line 3 and 4 once, saves tons of space. DominousSyndicate 41 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
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? Cloudwolf39 -13 — 5y
0
Ah i see what i did User#19524 175 — 5y
0
The third number should have been -1 User#19524 175 — 5y
0
thanks bro Cloudwolf39 -13 — 5y
Ad
Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago

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! :)

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? Cloudwolf39 -13 — 5y
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 chomboghai 2044 — 5y

Answer this question