I have this brick that is on fire it is supposed to burn the player and hurt the player over time
heres the script
function onTouched(part) local h = part.Parent:FindFirstChild("Humanoid") if h ~= nil then h.Health = h.Health - 1 end end script.Parent.Touched:connect(onTouched)
But when the player isn't touching the brick it will not harm the player
to get this straight too I the player touches the brick it will harm them bt as soon as the stop touching the brick it will stop harming them
https://www.roblox.com/games/540624890/2D-Adventure-NEW-RELEASE
To do what you want, make sure the part's being touched and while it's being touched do the code,
local part = script.Parent local function Check(c) local parts = c:GetChildren() local touching = part:GetTouchingParts()--Might be name for i,v in next, touching do for i2,v2 in next, parts do if v2:IsA("BasePart") then if v2 == v then return true; end end end end return false; end part.Touched:Connect:function(p) local h = p.Parent:FindFirstChild("Humanoid") if h then local x x = function() while wait(1) do if Check(p.Parent) and h.Health > 0 then h.Health = h.Health-1 else x:Disconnect() end end end spawn(x) end end)
One way to do so is to make a for
loop. We can get the player's health and make a decrement for
loop.
Here is how it's done!
local db = false local dbDelay = 3 function onTouched(part) local h = part.Parent:FindFirstChild("Humanoid") if db == true then return else if h ~= nil then db = true local count = 0 for i = h.Health, 0, -1 do h.Health = h.Health - 1 -- Subtracts the player's health by 1 every second. The number of times is based on the player's health. count = count + 1 if count == dbDelay then db = false end wait(1) end end end end script.Parent.Touched:connect(onTouched)
The debounce is very useful in order to avoid health decreasing too quickly. That's all I have in mind. If you have any problems, please leave a comment below. Thank you and I hope this will help you.
As you said in your comment, you want to subtract 1 health every second,
there are two main ways you can achieve this:
function onTouched(part) local h = part.Parent:FindFirstChild("Humanoid") if h ~= nil then while true do --sorry if I messed up on the while true do loop, haven't touched lua in a while h.Health = h.Health - 1 wait(1) end end end script.Parent.Touched:connect(onTouched)
function onTouched(part) local h = part.Parent:FindFirstChild("Humanoid") if h ~= nil then for = 100, 0, -1 do --sorry if I messed up how on the for loop, haven't touched lua in a while h.Health = i wait(1) end end end script.Parent.Touched:connect(onTouched)
Any questions, comments or concerns? Put them in a comment and I'll answer them when I get it!
If I did anything wrong, please put it down in the comments and I'll fix it :)
Are there any better methods to doing this? Make sure to comment to inform us all :)