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

Why is my brick not killing the player?

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

3 comments

1

You're only subtracting one health...

0

Error?

0

i want is to subtract 1 health every 1 second ik ill probably have to add a while true do statement tho

3 answers

✓ Accepted answer

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)

1 comment

0

Probably won't work. Don't feel like explaining.

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.

4 comments

0

yes it has the same issue ihave as soon as the player stops touching the brick it still harms the player whe they arent on the brick any fix to that?

0

I think there is a disconnect function, you'll need to look on the wiki

0

Probably a much better answer than mine. I'll leave mine up though. Feel free to downvote, anyone.

0

wait... i thought the asker wanted to keep the effect as soon as the player touches the brick. Srry...

As you said in your comment, you want to subtract 1 health every second,

there are two main ways you can achieve this:

  1. While true do loop
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)

  1. For loop
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 :)

1 comment

0

Don't know why it says 1. For loop. Whenever I go to edit it, it says 2, weird.

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