Im trying to create a mine, that if stepped on heals the player. But for some reason the script wont work.
Script
while wait(3) do
--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")
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
The only error i received from the output is " 23:16:49.036 Infinite yield possible on 'Workspace:WaitForChild("Humanoid")' - Studio
I tried fixing it by writing âif hitParent.Name == "Workspace" then return endâ but that didnt work
Help is greatly appreciated, thank you for your time
First off thereâs a few things I want to mention
Do not use :Connect()'s on often happening events in a while loop, this causes it to spawn often and things will then happen more often than you want them to
You didnât check whether the thing that touched it is a bodypart, the activator got touched by a part inside workspace and its parent is workspace, and obviously it canât find a âHumanoidâ directly inside workspace!
local sound = script:WaitForChild("Heal Sound")
local Mine_Model = nil
while task.wait(3) do
--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")
Activator.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
You can use a connect, youâll just need to disconnect it, you can do so like this:
local connection
connection = Part.Touched:Connect(function()
-- the function in here obviously
end)
-- once you want to stop it, you can run the following
connection:Disconnect()
-- for this case its probably easier if you declare 'connection' above the while loop
-- you can then access it from outside the while loop, or even from within the next while loop
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
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
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
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.
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.
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.