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)
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
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)
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.
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
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