Why wont this heal the player?

Ignore this post for the time.

1 Like
Activator.Touched:Connect(function(hit)
		--Heal
		if hit and hit.Parent then
           local humanoid = hit.Parent:FindFirstChild("Humanoid")
           if humanoid ~= nil and humanoid.Health > 0 then 
              -- do some stuff
           end
        end
	end)

don’t forget to add statements to confirm if it’s an actual humanoid or not

1 Like

so something like

connection = Activator.Touched:Connect(function(hit)
		--Heal
		local sound = script:WaitForChild("Heal Sound")		
		local hitParent = hit.Parent
		if hitParent.Name == "Workspace" then return end
		local humanoid = hitParent:WaitForChild("Humanoid")
		print(hitParent.Name.."_Has Healed!")
		print(humanoid.Health.."_<_"..humanoid.MaxHealth)
		print(hit, hitParent)
		if humanoid then
			if humanoid.Health <= humanoid.MaxHealth then
				sound:Play()
				humanoid.Health += 10
			end
		end
	end)



while true do
    --stuff
    connection:Disconnect(
end

No, more something like:

local connection


while wait(3) do
    if connection then
       connection:Disconnect()
    end
	--Variables
	local number1 = math.random(-50,0)
	local number2 = math.random(-50,0)
	local Mine_Model_2 = Mine_Model:Clone()
	--Setting the mines position 
	Mine_Model_2.Activator.Position = Vector3.new(number1, 0.15,number2)
	Mine_Model_2.Base.Position = Vector3.new(number1, 0.05,number2)
	Mine_Model_2.Parent = workspace --Same simplification as line 1 but with game.Workspace
	game.Debris:AddItem(Mine_Model_2, 5)  --Deletes the part after 3 seconds, replaces the Destroy() function
	
	local Activator = Mine_Model_2:WaitForChild("Activator")
	
	connection = Activator.Touched:Connect(function(hit)
		--Heal
		
		
		local sound = script:WaitForChild("Heal Sound")		
		local hitParent = hit.Parent
        if hitParent.Name == "Workspace" then return end

local humanoid = hitParent:WaitForChild("Humanoid")
		print(hitParent.Name.."_Has Healed!")
		print(humanoid.Health.."_<_"..humanoid.MaxHealth)
		print(hit, hitParent)
		if humanoid then
			if humanoid.Health <= humanoid.MaxHealth then
				sound:Play()
				humanoid.Health = humanoid.Health + 10
			end
		end
	end)
end
1 Like
Activator.Touched:Connect(function(hit)
	local Character = hit:FindFirstAncestorWhichIsA("Model")
	if Character then
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then


			--Heal
			local sound = script:WaitForChild("Heal Sound")	
			print(Character.Name.."_Has Healed!")
			print(Humanoid.Health.."_<_"..Humanoid.MaxHealth)
			print(hit, Character)
			if Humanoid.Health <= Humanoid.MaxHealth then
				sound:Play()
				Humanoid.Health = Humanoid.Health + 10
			end


		end
	end
end)

Better way to check if hit is a descendant of a Model, which could potentially be a character. No need for multiple ‘hit.Parent.Parent’ or whatever.

1 Like
local sound = script:WaitForChild("Heal Sound")
local Mine_Model = nil --reference mine_model here
local connection = nil

while task.wait(5) do
	local number1 = math.random(-50,0)
	local number2 = math.random(-50,0)
	local Mine_Model_2 = Mine_Model:Clone()
	Mine_Model_2.Activator.Position = Vector3.new(number1, 0.15, number2)
	Mine_Model_2.Base.Position = Vector3.new(number1, 0.05, number2)
	Mine_Model_2.Parent = workspace
	game.Debris:AddItem(Mine_Model_2, 4.5)
end

workspace.DescendantAdded:Connect(function(descendant)
	if descendant.Name == "Activator" then
		connection = descendant.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local humanoid = hit.Parent:WaitForChild("Humanoid")
				if humanoid then
					sound:Play()
					humanoid.Health += 10
				end
			end
		end)
	end
end)

workspace.DescendantRemoving:Connect(function(descendant)
	connection:Disconnect()
end)

Make it so that the loop isn’t repeating more frequently than the frequency at which new mines are being created.

Alright! Thank you!

How would i check if the hitParent is a bodypart? Would i use hitParent:IsA("BasePart")?

1 Like

if hit.Parent:FindFirstChild("Humanoid")

if hitParent:IsA("Model") and hitParent:FindFirstChild("Humanoid") then
   -- its a player 
end
1 Like

But what if the object that touches the mine doesnt have a model? (Like the players right leg)

The ancestor is a parent, or a parent of a parent, or even further, the leg’s first model ancestor is the character object

1 Like

OHHH, i read “Ancestor” as child. Thats my bad :sweat_smile:

FindFirstAncestorWhichIsA finds the first ancestor of a particular class, the left leg of a player’s character model has its first ancestor which is a model be the player’s character model itself.

1 Like

Oh, not to worry, I would still opt to use the Parent property though because you could have a part buried deep inside a model which would then trigger the event to fire if touched (unless the Humanoid instance check was also added).

Further there’s a slight difference, the script from @GhostShinjiro will also detect when it’s touched by a hat for example, while what I sent will only detect if the direct Parent of the part is the player’s character, which is true for all bodyparts, but not for hats

1 Like

Do you mean the part instance named “Handle” which belongs to the Accessory instance? As accessories themselves do not have a “Touched” event.

Idunno why everyone is overcomplicating things

local function ApplyTouchedEvent(Activator)
	local Ev
	Ev = Activator.Touched:Connect(function(hit)
		local Hmn = hit.Parent:FindFirstChild("Humanoid") --Use FindFirstChild instead of WaitForChild in cases like this
		if Hmn and Hmn.Health < Hmn.MaxHealth then
			Hmn.Health += 10 --Short for Hmn.Health = Hmn.Health + 10
			Ev:Disconnect() -- or the touched event automatically gets disconnected if the Activator object is destroyed
		end
	end)
end

while wait(3) do
--blahblah
	ApplyTouchedEvent(Activator)
end

Alternatively, if you just want it to apply to players, you can do if game.Players:GetPlayerFromCharacter(hit.Parent) then

1 Like

Yeah! i added the humanoid checker just now.

Humanoids, along with other body parts, need a parent that is a Model. So I don’t think you would need to worry about that. But I haven’t worked with custom characters so take this with a grain of salt.

1 Like

This is true, unless for some weird reason you have a rogue moving part (not inside a model) with a Humanoid instance parented to it.

2 Likes