Classic Sword not working after being scripted a little

Hi, I’m working on a simple sword fightning game but I have one issue: I have made it so you can pick up the sword by clicking on it. The picking up mechanic works fine, and the sword’s hit animation also work. But the problem is that it doesn’t deal any damage. I tried with an npc that has a Humanoid and I tried to kill it with the sword but it didn’t deal damage. I tried with the same sword from the toolbox but didn’t change it or anything, and that sword (from the toolbox) dealt damage on the npc.
I don’t know if the click to-pick up script I added in the sword handle might be messing with the pre-programmed sword script.
(Sorry if there is any spelling or grammar mistakes english is not my main language)

Here is my script:

local Tool = script.Parent.Parent
local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
	Tool:Clone().Parent = Player.Backpack

							
	
	
end)

script.Parent.CanTouch = false --Just to make the sword equipable only by clicking it, not touching it

Problem:

That’s because your sword handle can’t touch anything, therefore the Touched event won’t be fired (Touched event is the Attack function according to Roblox’s Linked Sword)

Solution:

Check if it has TouchInterest of the sword’s Handle and destroy it

Should look like this:

local Handle = script.Parent
local Tool = Handle.Parent
local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
	Tool:Clone().Parent = Player.Backpack
end)

local TouchInterest = Handle:FindFirstChildWhichIsA('TouchTransmitter')
if TouchInterest then TouchInterest:Destroy() end

When Touched event is listened/connected to the Handle then it will be created and fire the event. However, if you drop it then it would still create the same problem.

So you will need to make a task.wait() loop

Okay, I’m gonna try this now! :smile:

It worked now! Thank you so much :+1:

For each sword only:

local Handle = script.Parent
local Tool = Handle.Parent
local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
	Tool:Clone().Parent = Player.Backpack
end)

local function DestroyTouchInterests()
local TouchInterest = Handle:FindFirstChildWhichIsA('TouchTransmitter')
if TouchInterest then TouchInterest:Destroy() end
end
DestroyTouchInterests()

workspace.ChildAdded:Connect(function(Object)
spawn(function()
if Object == Tool then DestroyTouchInterests() end
end)
end)

Another thing you can do is copy the physical model from the sword out into your workspace. Keep the click detector and your script in it but remove the damage script.

Move the tool(with the damage script, but without your script) into ServerStorage.

Within your script then you can run:

local ss = game:GetService("ServerStorage")
local Tool = ss["the name of the sword tool"]
local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
	Tool:Clone().Parent = Player.Backpack
end)

This will prevent them from picking up the sword in workspace (as it is not a tool) however still clone the sword from ServerStorage over to their inventory.

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