Problem with a script for burning players

Hey I’m new to scripting and I’ve encountered a problem with a script that’s used for burning players. Whenever someone touches the brick its supposed to check who touched the brick then affect that player and allow other players to also receive damage if they touch it. The problem with this is that every time someone does touch the brick then they’ll receive damage but no one else is able to until the script finishes for the person who touched it. I’m using a server sided script for this. Help would be appreciated!

local dmgbrick = script.Parent
local debounce = false

dmgbrick.Touched:Connect(function(body)
	local player = game.Players:GetPlayerFromCharacter(body.Parent)
	local p = body.Parent.Torso.PlayerValue.Value
	
	if player.Name == p then
		local humanoid = body.Parent:FindFirstChild("Humanoid")
		
		if humanoid then
			if debounce then return end
			debounce = true
			local bodyid = body.Parent.Torso.fire_area
			local fire = Instance.new("Fire")
			
			fire.Name = "burn"
			fire.Size = 6
			fire.Parent = bodyid
			humanoid.Health = humanoid.Health - 25
			for i = 1, 5 do
				if humanoid.Health <= 5 then
					humanoid.Health = -2
				end
				humanoid.Health = humanoid.Health - 5
				fire.Size = fire.Size - 1
				task.wait(1)
			end
			fire:Destroy()
			debounce = false
		end
	end
end)

U need to thread this part because loops yield ( they stop the script from running anything else until the loop is finished )

for i = 1, 5 do
	if humanoid.Health <= 5 then
		humanoid.Health = -2
	end
	humanoid.Health = humanoid.Health - 5
	fire.Size = fire.Size - 1
	task.wait()
end

U could thread it by using task.spawn like this:

task.spawn(function()
-- code here
end)
1 Like

You’re using a simple boolean debounce, basically on or off. Once it is enabled by a player, it wont become disabled again until the damage loop stops.
To fix you can make the debounce variable a table and set a debounce variable for each player
EG:

local debounce = {}
if debounce[player.UserId] then 
return
end

debounce[player.UserId] = true
--damage player
debounce[player.UserId] = nil
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.