Touched event and Variables not changing

Hi, making a custom weapon (not a tool) that damages players it is touching when the player swings the sword. However, it is not damaging the player even when being swung. Animation plays.

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)

Try increase the time before changing CanHurt to false. It seems like it should work, you’re just not giving it enough time to let something be hit.

Tried it but still doesn’t work, I even tried using a boolvalue but it’s value is not being changed. Also tried removing changing Canhurt to false completely but still no damage

Where are you firing off that server event?

Client to Server, client checks whenever player left clicks.

Does it ever get to any of those print statements? Can we see the client code as well?

It works when I remove the

 and canhurt == true

CLIENT CODE:
Don’t see why this would help but ok

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("Swing")

local Player = game.Players.LocalPlayer --get the local player
local Mouse = Player:GetMouse() --get the mouse
 
Mouse.Button1Down:connect(function()
	Event:FireServer()
end)

If you print the children in Player.Character you can see sword, right? What happens when you print that if statement in the swing function?

what do u mean

Talking about: print(character:FindFirstChild("Sword“)) I’m wondering if it’s returning the sword, but the if statement isn’t going to the happy path because it isn’t getting back a boolean. It may not matter, but I’m curious.

yeah it prints

But does it ever get to print(“Swing”)?

nope. its not a bool, its still a variable in the script.

So can you try adding ~= nil after the character:FindFirstChild(“Sword”)- see if it gets to the print statement?

if character:FindFirstChild("Sword")~= nil then

Did this but it still executes everything after it

Can you try adding the same check to the if statement in the .Touched function below swing()? Do any of those statements get printed?

I have found many problems trying to set values inside of scripts I would advise trying it outside of the script set a bool value inside of the sword and set that from false to true to see if that makes a difference

Try doing something like

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


local function swing(Player)
	local character = Player.Character
	
if character:FindFirstChild("Sword") then
	print("Swing")
	wait(.05)
	canhurt = true
local animTrack = character.Humanoid.Animator:LoadAnimation(script.Anim)
animTrack:Play()
local connection = script.Parent.Touched:connect(function(p)
	if p.Parent:FindFirstChild("Humanoid") and canhurt == true
		then
		print('candamage')
  p.Parent.Humanoid:TakeDamage(10)
	end
	end)

wait(1)
connection:disconnect()
canhurt = false
end
end




Event.OnServerEvent:Connect(swing)

I’m on mobile so some things mighty be wrong, hard to proofread and I’m too lazy to type too much

1 Like

I have tried using a bool but it is the same thing, the value isnt changing

Will try this when I’m home, thanks