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)
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.
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)
@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.
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)