Ragdoll low health script doesn't work properly

So I have this script that works fine when you take damage, but once you regain it, the ragdoll does not stop, any help?

local plr = game.Players.LocalPlayer

local char = plr.Character

local hum = plr.Character.Humanoid

dog = false

while true do

if hum.Health <= 15 and dog == false then

dog = true

hum.WalkSpeed = 0

hum.JumpPower = 0

local setEnabled = hum:GetState() ~= Enum.HumanoidStateType.Physics

hum:ChangeState(setEnabled and Enum.HumanoidStateType.Physics or Enum.HumanoidStateType.GettingUp)

char.Animate.Disabled = setEnabled

wait()

end

if hum.Health >= 39 and dog == true then

dog = false

hum.WalkSpeed = 8

hum.JumpPower = 40

for _,v in pairs(hum:GetPlayingAnimationTracks()) do

v:Stop(0)

end

end

wait()

end
1 Like

Hey Pooglies,

Instead of using a while loop to constantly check your humanoid’s health,
use an event called HealthChanged which fires whenever the humanoids health changes.
You can use this in a way such as:

hum.HealthChanged:Connect(function()
–your logic for ragdoll
end)

As for your ragdoll issue, I’m not sure what the issue is but I can guarantee there’s no need to set Animate to disabled.

2 Likes

I just updated my post to my new issue, maybe it helps a little more? I did change the while true do to what you said though.

1 Like

I think your issue there now is that you do not reenable Animate. You see you only change it’s Disabled property while the player’s health is less than 15 and you only change their state when their health is less than 15.

You’d want to change their state in the below if statement ‘if hum.Health >= 39’ and reenable the Animate script.

So like this?

if hum.Health >= 39 and dog == true and setEnabled then

dog = false

hum.WalkSpeed = 8

setEnabled = false

hum.JumpPower = 40

for _,v in pairs(hum:GetPlayingAnimationTracks()) do

v:Stop(0)

end

end

wait()

end

end)

This did not work

No, SetEnabled is local to the top if statement scope. remove the ‘and setEnabled’ and inside the if statement you’ve just provided, set the humanoid’s state back to ‘GettingUp’ and reenable their Animate script.

if hum.Health >= 39 and dog == true and setEnabled then

dog = false

hum.WalkSpeed = 8

hum.JumpPower = 40

hum:ChangeState(Enum.HumanoidStateType.GettingUp)

char.Animate.Disabled = false

for _,v in pairs(hum:GetPlayingAnimationTracks()) do

v:Stop(0)

end

end

wait()

end

end)

Remove the ‘setEnabled’ in the if statement too.

Im not very good with all this stuff, im going off of an EchoReaper post.

I rmeoved the SetEnabled and its still not working, heres what I have

if hum.Health >= 39 and dog == true then

dog = false

hum.WalkSpeed = 8

hum.JumpPower = 40

hum:ChangeState(Enum.HumanoidStateType.GettingUp)

char.Animate.Disabled = false

for _,v in pairs(hum:GetPlayingAnimationTracks()) do

v:Stop(0)

end

end

wait()

end

end)

It might help by showing the script im going off of

local character = script.Parent
local humanoid = character.Humanoid

game:GetService("ContextActionService"):BindAction("Ragdoll me", function(_,input)
	if input == Enum.UserInputState.Begin then
		local setEnabled = humanoid:GetState() ~= Enum.HumanoidStateType.Physics
		humanoid:ChangeState(setEnabled and Enum.HumanoidStateType.Physics or Enum.HumanoidStateType.GettingUp)
		character.Animate.Disabled = setEnabled
		
		if setEnabled then
			for _,v in pairs(humanoid:GetPlayingAnimationTracks()) do
				v:Stop(0)
			end
		end
	end
end, false, Enum.KeyCode.R)

This is done in a LocalScript correct?

That is correct characterlimit30

Where about on the client is the script located?

StarterCharacterScripts char30

You seem to have missed out this part inside your code

But this is my updated code?

if hum.Health >= 39 and dog == true then

dog = false

hum.WalkSpeed = 8

hum.JumpPower = 40

hum:ChangeState(Enum.HumanoidStateType.GettingUp)

char.Animate.Disabled = false

for _,v in pairs(hum:GetPlayingAnimationTracks()) do

v:Stop(0)

end

end

wait()

end

end)

Yes, EchoReaper checks if SetEnabled is true and then stops all animation tracks inside the Humanoid from playing.

Where would I put this? Char30

Wrap your for loop in an if statement that checks if ‘setEnabled’ is true first, the for loop that loops through the animation tracks for the player.