Touched event and Variables not changing

Just tried it, animation plays but ‘candamage’ does not print and I still can’t damage

Try taking the code I posted and removing the and canhurt == true part

From looking at your code and where your bugs are arising, it seems like one of three issues:

  1. Your humanoid in the character is not named “Humanoid.” (Happened to me before)

  2. canhurt never updates to true.

  3. There might be a problem with the connection part? Try removing the local connection = as well as connection:disconnect() and going the old-fashioned route by adding a debounce.

Let me know if it still fails to execute.

Removed it, doesn’t damage the humanoid either

I made a new version but I’m still having the same problem when the animprogress is printed, it is constantly false. So the problem is with the variable.

local animTrack
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = Instance.new("RemoteEvent", ReplicatedStorage)
Event.Name = "Swing"
local animInProgress = false

local function playAnim(animTrack)
    animTrack:Play()
    animInProgress = true
    animTrack.Stopped:connect(function()
        animInProgress = false
    end)
end

local function swing(Player)
    local character = Player.Character
    
if character:FindFirstChild("Sword") then
    print("Swing")
    wait(.05)
    local animTrack = character.Humanoid.Animator:LoadAnimation(script.Anim)
    playAnim(animTrack)
    wait(.05)
    end
end

script.Parent.Touched:connect(function(p)
    if p.Parent:FindFirstChild("Humanoid") and animInProgress == true
        then
        print('candamage')
  p.Parent.Humanoid:TakeDamage(10)
    end
    end)


Event.OnServerEvent:Connect(swing)

At this point you might just want to open a test place, it’s starting to seem like we’re going in circles. There could be an issue with the animation, other code interfering, or the sword itself.

I have tested everything, its just that the variable isnt changing

Have you looked for exactly when the variable is changing? Obviously, it’s supposed to be true until the animation is done playing, but have you certified that it even is being set to true? You could make a function which prints every time the variable is changed.

function changeVar(toValue)
    print("Changing var to "..toValue)
    animInProgress = toValue
end

When I’m experiencing a problem like this, I tend to input prints at every critical point telling what a var is.

Its not being changed to true, idk why

I have edited the code and now, the variable is changing from true to false when you swing the sword. However, the damage is not working but it works when i remove candamage == true, so perhaps its changing too fast

CODE:

 local animTrack
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = Instance.new("RemoteEvent", ReplicatedStorage)
Event.Name = "Swing"
local animInProgress = false

local function playAnim(animTrack)
    animInProgress = true
    animTrack:Play()
    animTrack.Stopped:connect(function()
        print(animInProgress)
wait(10)
        animInProgress = false
    end)
end

local function swing(Player)
    local character = Player.Character
    
if character:FindFirstChild("Sword") then
    print("Swing")
    wait(.05)
    local animTrack = character.Humanoid.Animator:LoadAnimation(script.Anim)
    playAnim(animTrack)
    wait(.05)
    end
end

script.Parent.Touched:connect(function(p)
    if p.Parent:FindFirstChild("Humanoid") and animInProgress == true
        then
        print('candamage')
  p.Parent.Humanoid:TakeDamage(10)
    end
    end)


Event.OnServerEvent:Connect(swing)

If I’m correct, you are doing this in a local script? If you aren’t the animations will not function correctly but the sword should still do damage since you are doing it server side.