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

Script that supposed to decrease health every .2 seconds not working?

Asked by 2 years ago
local redpart = script.Parent
local Player = game.Players.LocalPlayer
local  character = Player:GetPlayerFromCharacter(character)
local humanoid = character:FindFirstChild("Humanoid")


redpart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        while humanoid.Health > 0 do
            wait(0.2)
            humanoid:TakeDamage(10)
        end
    end
end)

Ive tried doing this in global and local scripts but Player.Character kept erroring for me on global so just did local (my script isnt working on either though)

2 answers

Log in to vote
0
Answered by 2 years ago

Hello Bloxerventure!

You can only define player in local scripts and define if a part touched something in scripts. Here is a script that you should instead use in a global script

local redpart = script.Parent

redpart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        while hit.Parent.Humanoid.Health > 0 do
            wait(0.2)
            hit.Parent.Humanoid:TakeDamage(10)
        end
    end
end)
0
Thank you for your awnser but also would you happen to know how to stop the damage when TouchEnded tried doing this but wasnt working Bloxerventure 5 — 2y
0
I mean you could try to make a new bool value in the part and call it lets say 'Touched'. In the while loop you could say: while hit.parent.Humanoid.Health >0 and redpart.Touched.Value == false do. Another script could change the bool value in the touch ended function. ghostbettert 30 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

I don't understand why you need to do Player.Character or the other variables for a Touched event to damage someone but you can simply do this.

local redpart = script.Parent

redpart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local humanoid = hit.Parent.Humanoid
        while humanoid.Health > 0 do
            wait(0.2)
            humanoid:TakeDamage(10)
        end
    end
end)

Answer this question