Sword Script Error

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)

image

Try changing the Character = game.Workspace:FindFirstChild(plr.Name) to:

local Character = plr.Character

Also, anything that uses “game.Workspace” is deprecated. When referencing the workspace, use just “workspace”, not “game.Workspace”

Maybe add a check for the tool as well? Something like:

local Character = plr.Character
local Sword = Character:FindFirstChild("ClassicSword")

if Sword == nil then return end

Then after the sword check then you can put the rest of the function.

1 Like

Use :FindFirstChildWhichIsA(“Tool”) as your sword variable, so here is an example

local Sword = mycharacter:FindFirstChildWhichIsA(“Tool”)

if Sword then
print(“player has a sword equipped”)
end

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.