How to make damage brick only activate once before making the player jump?

I am trying to create a damage brick that, when touched by a player, causes them to jump.
However, the script damages the target an inconsistent number of times, even when the target stops touching the part by jumping.

local hurtBrick = script.Parent
script.Parent = game.ServerScriptService
hurtBrick.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Jump = true
		hit.Parent.Humanoid.Health = (math.floor(hit.Parent.Humanoid.Health*0.8)*4)*0.25
	end
end)

Thanks in advance.

Have you considered looking at a debounce? This can be used to prevent an action from running an unnecessary amount of times, and it’s not hard to create one

Also, why are you parenting your script in ServerScriptService where you could just place it manually instead? (Small nitpick)

local HurtBrick = workspace:WaitForChild("HurtBrick") -- Obviously you'll want to replace this with your Brick
local Debounce = false -- We want to make this a Bool, so that we can easily check for 2 values
local DBRate = 1 -- This is the rate for how often the Debounce should fire (In Seconds)

local function Touched(Hit)
    local Target = Hit.Parent

    if not Target or Debounce then return end -- What this will check, is if our "Debounce" variable is set to "true" as an already running action or not & for the Hit's Parent as well and if any of these 2 match up, we'll cancel the function early

    local Hum = Target:FindFirstChildOfClass("Humanoid")

    if Hum and Hum.Health > 0 then
        Debounce = true -- After verifying our sanity checks, we can then set this to "true"

        Hum.Jump = true
        Hum.Health = (math.floor(Hum.Health * 0.8) * 4) * 0.25

        task.wait(DBRate)
        Debounce = false
    end
end

HurtBrick.Touched:Connect(Touched)

Debounces are really handy to have to ensure we don’t keep firing the same action a fast amount of times

Unfortunately there’s still desynchronization between the damage and jumping, and sometimes it makes the player jump without damaging them or damaging them without making them jump.
As for moving the script to ServerScriptStorage I figured it would be easier to find what script effects each object, but I’d like to hear about a better method if there is one.

1 Like

Update: Nevermind - after a bit of adjustment to the debounce rate it’s working fine.

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