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

How do I stop the player's health from regenerating?

So I was creating this script in which the player's camera will shake every time they take damage. It works, but the thing is that when the player regenerates health, the camera shake plays.

I couldn't think of a better way of making this portion of the script, so I decided I would completely halt the player's regeneration.

Here is my portion of the script, if you would like to fix/polish off-topic.

--the variables are already set for this portion

humanoid.HealthChanged:Connect(function()
	for i = 1,5 do
		humanoid.CameraOffset = Vector3.new(x,y,z)
		wait()
	end
	humanoid.CameraOffset = Vector3.new(0,0,-1)
end)

So if you have any suggestions/answers, feel free to answer my question. I really need it.

2 answers

✓ Accepted answer

If you don't mind, I'll propose a solution that will allow your camera shake system to work while still allowing the player to regenerate.

Essentially we need to know how the health has changed. To do this, we'll constantly update the player's health into a variable like last_health_update, then take the difference of this value and the new health value whenever HealthChanged is called:

local last_health_update = humanoid.Health -- initialize to be 100

humanoid.HealthChanged:Connect(function(new_health)
	local delta = last_health_update - new_health -- how the player's health has changed
	if delta > 0 then -- if the player has lost health
		-- Do your camera shake stuff
	end
	last_health_update = new_health -- update last_health_update var
end)

P.S.: Crazyman32 has a very fun Camera Shake module that might interest you https://www.roblox.com/library/1461025953/Camera-Shaker

2 comments

0

cool

0

Thank you for responding! You fixed my question like a boss. Really appreciated, thanks again.

You just simply insert an empty script named “Health” into PlayerStarterScripts. The game checks if it exists at first and if it doesn’t, then it inserts it’s own “Health” script which has the regeneration.

1 comment

0

Can you show me more? Perhaps you can show me an example.

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