So basically I want players to damage characters when they attack.
The only problem is… It only emits once. Even the other touched event is assigned back when players attack.
here is my script
game.ReplicatedStorage.Attack.OnServerEvent:Connect(function(player, damage, tool, charge, swing, hitId, lightId, heavyId, lightId2)
local event
playSound(swing, tool)
local lightAttack = Instance.new("Animation")
lightAttack.Name = "LightAttack"
lightAttack.Parent = game.Workspace
lightAttack.AnimationId = lightId
lightAttack = player.Character.Humanoid.Animator:LoadAnimation(lightAttack)
local secondLightAttack = Instance.new("Animation")
secondLightAttack.Name = "SecondLightAttack"
secondLightAttack.Parent = game.Workspace
secondLightAttack.AnimationId = lightId2
secondLightAttack.Parent = player
secondLightAttack = player.Character.Humanoid.Animator:LoadAnimation(secondLightAttack)
local heavyAttack = Instance.new("Animation")
heavyAttack.Name = "HeavyAttack"
heavyAttack.Parent = game.Workspace
heavyAttack.AnimationId = heavyId
heavyAttack = player.Character.Humanoid.Animator:LoadAnimation(heavyAttack)
local chosen
if charge == 10 then
local possibility = {lightAttack, secondLightAttack}
local chosenOne = possibility[math.random(1, #possibility)]
chosen = chosenOne
else
chosen = heavyAttack
end
game.ReplicatedStorage.DecreaseStamina:Fire(player, charge) -- Firing bindable event to decrease player's stamina
event = tool.HitPart.Touched:Connect(function(hit)
print("Touched")
if hit:FindFirstAncestorOfClass("Model") then -- Few checks to see if that is an NPC or a Player
print("It is a model!")
local character = hit:FindFirstAncestorOfClass("Model")
print(character.Name)
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
print("It is humanoid")
if player.Stamina.Value >= charge then
print("Damaging the char")
humanoid:TakeDamage(damage) -- Make the player damaged.
playSound(hitId, tool)
event:Disconnect()
end
end
end
end)
chosen:Play()
repeat wait() until chosen.IsPlaying == false
print("Stopped!")
event:Disconnect()
end)
Expected output when it touched for the first time.
Touched
It is a model!
It is humanoid
Damaging the char
Stopping!
Expected output when it touched for the second time.
Stopping!