I am trying to make a sword gear you can swing with a click. It is supposed to do 10 damage per slash and play an animation.
This error occurs when I equip the sword in playtest and click with it.
I have not tried anything yet as I got this script from a tutorial since I am not very experienced with scripting.
I’ve pasted the entire script below as well as the explorer if it is needed.
local SwordEvent = game.ReplicatedStorage:WaitForChild("SwordEvent")
local SoundService = game.SoundService
local SwordSlash1Animation = Instance.new ("Animation")
SwordSlash1Animation.AnimationId = "rbxassetid://15669471866"
local SwordSlash2Animation = Instance.new ("Animation")
SwordSlash2Animation.AnimationId = "rbxassetid://15669473926"
local PlayersWhoCanDamage = {}
local function AnimationLoader(Animation, Character)
local AnimationTrack = Character.Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
SoundService["Sword Sound"]:Play()
end
local function Damage(CharacterToDamage)
CharacterToDamage.Humanoid.Health -= 10
SoundService["Sword Sound"]:Stop()
SoundService["Sword Stab"]:Play()
end
SwordEvent.OnServerEvent:Connect(function(plr, attacktype)
local Character = game.Workspace:FindFirstChild(plr.Name)
if attacktype == "Slash1" then
AnimationLoader(SwordSlash1Animation, Character)
table.insert(PlayersWhoCanDamage, plr.Name)
Character.ClassicSword.Handle.Touched:Connect(function(Hit)
if table.find(PlayersWhoCanDamage, plr.Name) and Hit.Parent:FindFirstChild("Humanoid") then
Damage(Hit.Parent)
table.remove(PlayersWhoCanDamage, table.find(PlayersWhoCanDamage, plr.Name))
end
end)
wait(0.8)
if table.find(PlayersWhoCanDamage, plr.Name) then
table.remove(PlayersWhoCanDamage, table.find(PlayersWhoCanDamage, plr.Name))
end
elseif attacktype == "Slash2" then
AnimationLoader(SwordSlash2Animation, Character)
table.insert(PlayersWhoCanDamage, plr.Name)
Character.ClassicSword.Handle.Touched:Connect(function(Hit)
if table.find(PlayersWhoCanDamage, plr.Name) and Hit.Parent:FindFirstChild("Humanoid") then
Damage(Hit.Parent)
table.remove(PlayersWhoCanDamage, table.find(PlayersWhoCanDamage, plr.Name))
end
end)
wait(0.8)
if table.find(PlayersWhoCanDamage, plr.Name) then
table.remove(PlayersWhoCanDamage, table.find(PlayersWhoCanDamage, plr.Name))
end
end
end)