Heal system (click)

hello there, I’m have a script for the heal system but the script isn’t truly work by read the script that I have display under this, in the print(“click”) it say the script ended there and cant continue the script, I don’t really know what wrong but I think I was cuz by the local to find the player humanoid. Here the script.

local healthPack = script.Parent
local healAmount = 30
local cooldown = 10

print("start")
healthPack.ClickDetector.MouseClick:Connect(function(plr)	
	print("click")-- the script stop here it wont go or print 
	local player = game.Players.PlayerAdded
    local char = player.Character or player.CharacterAdded	
    local humanoid:Humanoid = player.Character or player.CharacterAdded:WaitForChild("Humanoid")
	local maxhp = humanoid.MaxHealth
print("player data")
			local canheal = humanoid.Health < maxhp
	        local cantheal = humanoid.Health >= maxhp
	
		print("going to heal player")
		         if plr and canheal then
			humanoid.Health = humanoid.Health + healAmount
			repeat until 
			humanoid.Health + healAmount
print("finish heal")
		end
	end)

if want further info to help fix this, I gradually tell you.

1 Like

I’m not sure why you made a second variable to the player? Just switch all of the 'player’s to ‘plr’

local healthPack = script.Parent
local healAmount = 30
local cooldown = 10

print("start")
healthPack.ClickDetector.MouseClick:Connect(function(plr)	
	print("click")-- the script stop here it wont go or print 
    local char = plr.Character or plr.CharacterAdded	
    local humanoid:Humanoid = plr.Character or plr.CharacterAdded:WaitForChild("Humanoid")
	local maxhp = humanoid.MaxHealth
print("player data")
			local canheal = humanoid.Health < maxhp
	        local cantheal = humanoid.Health >= maxhp
	
		print("going to heal player")
		         if plr and canheal then
			humanoid.Health = humanoid.Health + healAmount
			repeat until 
			humanoid.Health + healAmount
print("finish heal")
		end
	end)
1 Like

I will also fix some mistakes in your post.

plr.CharacterAdded is an event. You should either use :Connect or :Wait. In this case, you will use :Wait()

You already have the character, why are you gonna index it again? And also, plr.Character is the Character, not the humanoid. :man_facepalming:

And for @Ryan_Danial,

what does this achieve?

cantheal was never used, plus canheal already checks if the health is less than MaxHealth.
And also, if you didn’t know, Humanoid.Health can never be more than its MaxHealth. If it is, then it will automatically set the health to max health.

2 Likes
healthPack.ClickDetector.MouseClick:Connect(function(plr)	
	print("click")-- the script stop here it wont go or print 
	local player = game.Players.PlayerAdded
    local char = plr.Character or plr.CharacterAdded	
    local humanoid:Humanoid = plr.Character:WaitForChild("Humanoid")
	print("going to heal player")  
	if plr then
			humanoid.Health = humanoid.Health + healAmount
	end
end)

change script but the healing doesn’t work

can you try this

healthPack.ClickDetector.MouseClick:Connect(function(plr)
	print("click")
	local humanoid:Humanoid = plr.Character and char:FindFirstChild("Humanoid")
	print(humanoid)
	if humanoid then
		print("healing")
		humanoid.Health = humanoid.Health + healAmount
		print("healed")
	end
end)
2 Likes