So i have this script that i’m trying to make, it suppose to damage the closest humanoid when the Coin’s health is changed but it for some reason it just didn’t work
this the script that im having trouble with
local Coin = script.Parent
local Damage = Coin:FindFirstChild("Damage")
local DontDamageThisHumanoidValue = Coin:FindFirstChild("DontDamageThisHumanoidValue")
local Humanoid = Coin:FindFirstChildOfClass("Humanoid")
local HumanoidRootPart = Coin:FindFirstChild("HumanoidRootPart")
if Humanoid then
CurrentHealth = Humanoid.Health -- Saves the cuurent Health
Humanoid.HealthChanged:Connect(function(Health)
if Health <= 0 or Health >= CurrentHealth then
return
end
local ClosestHumanoid = nil
local ClosestDistance = 90 -- Range
-- Detects is its in the range
for _, CharacterModel in pairs(workspace:GetDescendants()) do
if CharacterModel.Parent and CharacterModel.Parent:IsA("Model") then
local CharacterHumanoid = CharacterModel.Parent:FindFirstChildOfClass("Humanoid")
local TargetHumanoidRootPart = CharacterModel.Parent:FindFirstChild("HumanoidRootPart")
if CharacterHumanoid and CharacterHumanoid.Health > 0 and CharacterHumanoid.Name ~= DontDamageThisHumanoidValue.Value then
local HumanoidDistance = (TargetHumanoidRootPart.Position - HumanoidRootPart.Position).Magnitude
if HumanoidDistance < ClosestDistance then
ClosestHumanoid = CharacterHumanoid
ClosestDistance = HumanoidDistance
end
end
end
end
if ClosestHumanoid then
local healthLost = math.floor(CurrentHealth - Health) -- Detects how much damage it Gotten
Damage.Value = healthLost + healthLost -- 2x the number the damage
ClosestHumanoid:TakeDamage(Damage.Value) -- Takes Damage on the player
print(Damage.Value)
Coin:Destroy()
end
end)
end