Issues with putting a function on running every time the health changed

Okay so I made a script for stuffs that I wanted to do. So basicly whenever the new health of a humanoid is lower than the old health it will run, But It kept running, I don’t know how to make it only run once everytime the health changes.
Here’s the script.

local remote = script.Parent:WaitForChild("RemoteEvent")
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Old_Health = player.Character:WaitForChild("Humanoid").Health
local Cd = false


player.Character.Humanoid.HealthChanged:Connect(function(Newhealth)
	if Newhealth < Old_Health and not Cd then
		remote:FireServer()
	
	end
end)
1 Like

Ok, so it is very hard to explain, but I’ll try to break it down.
So there’s this thing called debounce, and at the start you should put local debounce = false, and with the if statement, you should do
if Newhealth < Old_Health and not Cd and debounce = false then
and make it so inside the string (I think that’s what it is called, or inside the if statement,) you should make it so that if the health doesn’t change, then debounce = true and if it does change, then it becomes false.

1 Like

Oh I never thought of that. Thanks, I know what a debounce is so I will try to script it.

1 Like

So basically, I recommend searching up how debounce works by AlvinBlox or any other good YouTuber for scripting about it, or searching up what debounce is if you don’t know.
With this information about debounce you should make it so that the script runs when debounce = true, or when the health changes.

Oh you already know sorry about the extra comment, but good luck and hope ur scripting goes well!

1 Like

Alright. I am very slow at writing so by the time I finished this, the issue has been solved, BUT there is still a flaw in your script.

If you are using a remote event, always put it somewhere where it can be accessed by BOTH the server & the client. I can see by the code
local remote = script.Parent:WaitForChild("RemoteEvent") that the RemoteEvent and the local script are in one place. Which shouldn’t be possible. If the remote event is somewhere in the client’s GUI or player scripts then the server script won’t be able to find the remote event and receive the info. If the local script is in ReplicatedStorage it wont work at all!

So place the local script in the player’s StarterPlayerScripts for example and the remote event somewhere in ReplicatedStorage.

local remote = game.ReplicatedStorage.healthChanged -- remote event for server script
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(char)
	local Old_Health = player.Character:WaitForChild("Humanoid").Health -- by default its always going to be 100
	local Cd = false
	
	player.Character.Humanoid.HealthChanged:Connect(function(newHealth)
		if newHealth < Old_Health and not Cd then
			remote:FireServer()
			Old_Health = newHealth -- update the health to always be the previous value (if u want)
		end
	end)
end)

I hope this somewhat helped, I noticed this issue before I could even address the fact that you needed a debounce haha, sorry about that!

I also thought it would make sense to update the Old_Health value everytime there is a new health change, so that the old health value is updated to the previous value.

And I put it all in a CharacterAdded event, because I’m pretty sure its more efficient.

1 Like

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