Healing part not healing properly

I new to lua, sorry for the inconvenience.
Okay so, I’m trying to make a part that heals you if you touch it. The script does work, except for the healing part. Can someone help me? Thanks.

local heal = script.Parent

heal.Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") then
		otherPart.Parent.Humanoid.Health = 100
		heal.CanCollide = false
		heal.Transparency = 0.7
		wait(4)
		heal.CanCollide = true
		heal.Transparency = 0
	end
end)

There’s no errors on the output

So I assume you’re trying to heal the person to full health. Okay, this is a pretty good script, and I think you’re trying to disable the heal part. CanCollide only changes collision properties with the player (big words, ik.): player moves through part with cancollide off.
Use a debounce like:

local heal = script.Parent
local Debounce = false
heal.Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") and not Debounce then
        Debounce = true
		otherPart.Parent.Humanoid.Health = otherPart.Parent.Humanoid.MaxHealth
		heal.Transparency = 0.7
		wait(4)
		heal.Transparency = 0
        Debounce = false
	end
end)

Also, when you make it cancollide off, it slides/goes through everything, so if it’s unanchored, it will drop through the floor, and the game. So anchor it if you don’t want it to fall out of the world. If you want cancollide off, feel free to do so.

Thanks for the advice, I appreciate it. But what do I do if I want them to heal by a certain amount?

local heal = script.Parent
local Debounce = false
heal.Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") and not Debounce then
        Debounce = true
		otherPart.Parent.Humanoid.Health += 50 --Amount
		heal.Transparency = 0.7
		wait(4)
		heal.Transparency = 0
        Debounce = false
	end
end)

The healing part of the script still doesn’t work. Maybe this is a studio glitch

@Bikereh gave a perfect example. Using the
+= and -= identifiers, you can keep the same amount, but minus. Let me explain what that does.

Doing:

Health += 10

Just does

Health = Health + 10

Easier and more efficient, and fun to use.

You might not understand this know, but I’m sure you will in the future. Almost absolutely everything is object orientated. You change properties. You use events to isolate when to change those properties. You use functions to do those things. That’s the main thing with roblox lua. You change properties, to move stuff, to make things look cool. Everything is all object, and property based. (except for a few examples). I’m sure you will see this later, if not now.

try this

local heal = script.Parent
local Debounce = false
heal.Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") and not Debounce then
        Debounce = true
		otherPart.Parent.Humanoid.Health = otherPart.Parent.Humanoid.Health + 50 -- Amount
		heal.Transparency = 0.7
		wait(4)
		heal.Transparency = 0
        Debounce = false
	end
end)

Still doesn’t work… Is there possibly another code you have to use like TakeDamage()?

Still doesn’t work either, Is there possibly another code you have to use like TakeDamage()?

are there any errors in the output

There’s no errors. The can collide and the transparency part of the script works, its only the healing part that’s broken.

well then is the script local or server

its a normal script that I placed under the heal part

maybe this?

local heal = script.Parent
local Debounce = false
local healAmount = 50
heal.Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") and not Debounce then
		Debounce = true
		if otherPart.Parent.Humanoid.Health < otherPart.Parent.Humanoid.MaxHealth then
			if (otherPart.Parent.Humanoid.Health + healAmount ) < otherPart.Parent.Humanoid.MaxHealth then
				otherPart.Parent.Humanoid.Health = otherPart.Parent.Humanoid.Health + healAmount
			else
				otherPart.Parent.Humanoid.Health = otherPart.Parent.Humanoid.MaxHealth
			end
		end
		heal.Transparency = 0.7
		wait(4)
		heal.Transparency = 0
		Debounce = false
	end
end)
end)

Can you put a print on the touch event and print otherPart?

There is a property of a BasePart that I recommend you use instead of Debounce called CanTouch.

If the CanTouch Property is false then no Touch Events will be registered when the basepart is touched which leads to a small performance boost
https://developer.roblox.com/en-us/api-reference/property/BasePart/CanTouch

local HealPart = script.Parent
local HealAmount = 50
HealPart.Touched:Connect(function(OtherPart)
    local Humanoid = OtherPart.Parent:FindFirstChildWhichIsA("Humanoid")
    if Humanoid then
        HealPart.CanTouch = false
        -- Humanoid.Health = Humanoid.MaxHealth
        -- Humanoid.Health += HealAmount
        heal.Transparency = 0
        task.wait(10)
        heal.Transparency = 0
        HealPart.CanTouch = true
    end
end)

Edit: If this doesn’t work you will have to disconnect the event and then reconnect it after instead.

2 Likes