Hello, i would like some help in this script i wrote. It works but i want for it to detect when the Hit.parent is dead. If so, it changes the team of the player who got the sword.
The if Humanoid.Died line doesn’t work and i would like help
local Turn = 1
local Damage = 12
local Cooldown = 1
script.Parent.Activated:Connect(function()
if script.Parent.Cooldown.Value == false then
script.Parent.Cooldown.Value = true
script.Parent.CanDamage.Value = true
local Humanoid = script.Parent.Parent.Humanoid
local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
script.Parent.Sound:Play()
if Turn == 1 then
Anim1:Play()
Turn = 2
Anim1.Stopped:wait(Cooldown)
script.Parent.CanDamage.Value = false
script.Parent.Cooldown.Value = false
elseif Turn == 2 then
Anim2:Play()
Turn = 1
Anim2.Stopped:Wait(Cooldown)
script.Parent.CanDamage.Value = false
script.Parent.Cooldown.Value = false
end
else
return
end
end)
script.Parent.Handle.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent.Humanoid
if script.Parent.CanDamage.Value == true then
Humanoid:TakeDamage(Damage)
script.Parent.Swing:Play()
script.Parent.sword.ParticleEmitter:Emit(1)
script.Parent.CanDamage.Value = false
if Humanoid.Died then
print("Death")
end
else
return
end
else
return
end
end)
Did you connected Humanoid.Died event to the print? I think this is why the code is uncompleted. It must’ve be done like Humanoid.Died:Connect(function().
I tried this block of your code and it worked fine:
if Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent.Humanoid
if script.Parent.CanDamage.Value == true then
Humanoid:TakeDamage(Damage)
script.Parent.Swing:Play()
script.Parent.sword.ParticleEmitter:Emit(1)
script.Parent.CanDamage.Value = false
if Humanoid.Died then
print(Hit.Parent.Name.." Died")
end
else
return
end
else
return
end
local Turn = 1
local Damage = 12
local Cooldown = 1
script.Parent.Activated:Connect(function()
if script.Parent.Cooldown.Value == false then
script.Parent.Cooldown.Value = true
script.Parent.CanDamage.Value = true
local Humanoid = script.Parent.Parent.Humanoid
local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
script.Parent.Sound:Play()
if Turn == 1 then
Anim1:Play()
Turn = 2
Anim1.Stopped:wait(Cooldown)
script.Parent.CanDamage.Value = false
script.Parent.Cooldown.Value = false
elseif Turn == 2 then
Anim2:Play()
Turn = 1
Anim2.Stopped:Wait(Cooldown)
script.Parent.CanDamage.Value = false
script.Parent.Cooldown.Value = false
end
else
return
end
end)
script.Parent.Handle.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent.Humanoid
if script.Parent.CanDamage.Value == true then
Humanoid:TakeDamage(Damage)
script.Parent.Swing:Play()
script.Parent.sword.ParticleEmitter:Emit(1)
script.Parent.CanDamage.Value = false
-- Check if the Hit.Parent has died
Humanoid.Died:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if player then
local team = player.TeamColor
-- Change the player's team to a new team
if team == BrickColor.new("Bright red") then
player.TeamColor = BrickColor.new("Bright blue")
else
player.TeamColor = BrickColor.new("Bright red")
end
end
end)
else
return
end
else
return
end
end)
´´´
Try this; I used “GetPropertyChangedSignal” Instead. It waits for a specific property to be changed.
local Turn = 1
local Damage = 12
local Cooldown = 1
script.Parent.Activated:Connect(function()
if script.Parent.Cooldown.Value == false then
script.Parent.Cooldown.Value = true
script.Parent.CanDamage.Value = true
local Humanoid = script.Parent.Parent.Humanoid
local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
script.Parent.Sound:Play()
if Turn == 1 then
Anim1:Play()
Turn = 2
Anim1.Stopped:wait(Cooldown)
script.Parent.CanDamage.Value = false
script.Parent.Cooldown.Value = false
elseif Turn == 2 then
Anim2:Play()
Turn = 1
Anim2.Stopped:Wait(Cooldown)
script.Parent.CanDamage.Value = false
script.Parent.Cooldown.Value = false
end
else
return
end
end)
script.Parent.Handle.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent.Humanoid
if script.Parent.CanDamage.Value == true then
Humanoid:TakeDamage(Damage)
script.Parent.Swing:Play()
script.Parent.sword.ParticleEmitter:Emit(1)
script.Parent.CanDamage.Value = false
Humanoid:GetPropertyChangedSingnal("Health"):Connect(function(Health)
if Health <= 0 then
print("Death")
end
end)
else
return
end
else
return
end
end)