Reassigned health kills you?

So I’m trying to create a block mechanic, when you hold the “R” key your health will be buffed, only for as long as you have the R Key held.

However, when it comes to letting go the R Key, it just kills you…

tool.RemoteEvent.OnServerEvent:Connect(function(player, state)
	
	local healthbefore = nil
	
	if state == "BLOCKING" then
		
		healthbefore = workspace[player.Name].Humanoid.Health
		
		print(healthbefore)
		
		tool.CanAttack.Value = false
		
		wait()
		
		workspace[player.Name].Humanoid.MaxHealth = 250
		workspace[player.Name].Humanoid.Health = 250
		
	end
	
	if state == "NOT BLOCKING" then

		tool.CanAttack.Value = true
		
		workspace[player.Name].Humanoid.Health = healthbefore
		workspace[player.Name].Humanoid.MaxHealth = 100

	end
	
end)

So the “healthbefore” value is so they dont just spam R to get back to 100 health if they’re damage, and the print properly says that the healthbefore value is above 0. But when they let go of R, it kills them… anyone know why?

the healthbefore value is assigned inside the Event, (meaning each time the remote is fired, the value will be set to nil which is converted to 0)

2 Likes

God damnit, what a simple mistake :man_facepalming: , thanks a ton.