On Critical Health script not working

Hello. I am looking towards making it so if someone’s HP is 50 and below, you would get a critical message and make it stun your character.

Also, by removing the HumanoidRootPart from the player temporarily, that make them stuns in some sort.

Script:

game.Players.PlayerAdded:Connect(function(plr)
	workspace[plr.Name].Humanoid.Health.Changed:Connect(function()
		if workspace[plr.Name].Humanoid.Health <= 50 then
			workspace[plr.Name].HumanoidRootPart.Name = plr.Name.."-CriticalHumanoidRootPart"
			workspace[plr.Name][plr.Name.."-CriticalHumanoidRootPart"].Parent = game:GetService("ServerStorage").RootParts
			plr.PlayerGui.Core.Alert.Visible = true
			plr.PlayerGui.Core.Alert.Text = "Critical Health"
			plr.PlayerGui.Core.Frozen.Visible = true
			plr.PlayerGui.Core.Frozen.Text = "You are currently in critical conditions. You will automatically be revived when your in medium conditions."
			while wait(0.2) do
				if plr.Humanoid.Health >= 50 then
					plr.PlayerGui.Core.Alert.Visible = false
					plr.PlayerGui.Core.Frozen.Visible = false
					game:GetService("ServerStorage").RootParts[plr.Name.."-CriticalHumanoidRootPart"].Parent = workspace[plr.Name]
					workspace[plr.Name][plr.Name.."-CriticalHumanoidRootPart"].Name = "HumanoidRootPart"
					return	
				end
			end
		end
	end)
end)

Changing core object like the HumanoidRootPart is not recommended, and does not serve any practical purpose.

You also cannot access the Player’s GUI object from a ServerScript.

Set up a Client Script to read the health and edit the gui as you have above.

As for stunning, you can do this a few ways.

  • Set Humanoid > PlatformStand to make them fall
  • Setting JumpPower & WalkSpeed to 0
  • Unbinding the Client movement system and rebinding when not stunned

As another note - this will fail to work when your player respawns as you’re not triggering this code when the Character is Added

Use the following before grabbing the character’s humanoid

plr.CharacterAdded:Connect(function(char)
	local hum = char.Humanoid
	hum.HealthChanged:Connect(function()
		if (hum.Health < 50) then
			hum.PlatformStand = true
		end
	end)
end)

As for the PlatformStand it is a simple Boolean value (true / false),
@achdef

One last thing - you cannot connect to a specific Property like this:
hum.Health.Changed:Connect ...
However, there is an event for checking this.
hum.HealthChanged:Connect ...
API Reference for Humanoid
Humanoid (roblox.com)

1 Like

Use break instead of return(return “breaks” the entire connection above the loop not the specific loop).

1 Like

How would I set the platformstand? Like to 0?

1 Like

I managed to clean up the old code sample, and fix the issues(by storing important objects into variables and fixing typos):

--Script inside StarterCharacterScripts
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

--When a script is inside StarterCharacterScripts it gets parented inside the player character(each time they load).
local Character = script.Parent 
local Player = Players:GetPlayerFromCharacter(Character)
--store and wait for the humanoid, to avoid errors
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")

--Health.Changed isn't an event, HealthChanged and :GetPropertyChangedSignal("Health") is.
Humanoid.HealthChanged:Connect(function(health) --new health
	local Core = Player.PlayerGui.Core 
	if health <= 50 then 
		Root.Name = Player.Name.."-CriticalHumanoidRootPart"
		Root.Parent = ServerStorage.RootParts
		Core.Alert.Visible = true
		Core.Alert.Text = "Critical Health"
		Core.Frozen.Visible = true
		Core.Frozen.Text = "You are currently in critical conditions. You will automatically be revived when your in medium conditions."
	--because we are checking every time the health changes, the loop isn't needed	
	else --if health is above 50
		Core.Alert.Visible = false 
		Core.Frozen.Visible = false 
		Root.Parent = Character 
		Root.Name = "HumanoidRootPart" 
	end
end)
1 Like

Thank you! I’ll use this. Now I got two solutions.