Why isn't my sword Damage script not working?

You can write your topic however you want, but you need to answer these questions:
I’m trying to make a damage script for my sword but it’s not taking any damage, what I trying to do is enabling the damage script when the animation is playing then disable it when it’s finished

I’ve tried locating the scripts in different areas but I still go no luck :confused:

I have a local script handling the animation and a Server script handling the damage

--Local Script
local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://5940472953" --Animation ID
local track
local canSwing = true
local DamageScript = script.Parent.Handle["Damage"]

tool.Activated:Connect(function()
	
	if canSwing then
		canSwing = false
		track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
		track.Priority = Enum.AnimationPriority.Action
		DamageScript.Disabled = false
		track:Play()
		wait(1)
		DamageScript.Disabled = true
		canSwing = true
		
	end

end)
tool.Unequipped:Connect(function()
    if track then
        track:Stop()
    end
end)

This is is the Server Script for the Damage


--Server Script
function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human:TakeDamage(8.5)
        end
end
script.Parent.Handle.Touched:connect(onTouched)

Thanks :heart:

Try changing the local script to a script.
Also, try to put print("something") in the server script so you can see if something touched the part.
If that doesn’t work, try this :

script.Parent.Handle.Touched:Connect(function(hit)
    print("This thing touched the part :"..hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid:TakeDamage(8.5)
    end
end)
1 Like

Alright I will try that out. I’ll let you know if that works

1 Like

Hmm, what do you think is in the error output? Some bug can happen because the holder of that tool may have a humanoid instance named differently than “Humanoid” or something else.

Anyways, the local script disables and enables the server script right? That only happens for the player, not for everyone, you should add a bool value inside the tool, make the local script fire a remote then the regular script would get the event, get the delivered variable and then damage the user.

1 Like

Here’s what i mean the damage script will only happen for the player who did it, your disabling and enabling the damage script in the local script, only the local script can connect to one player meaning it won’t be disabled or enabled in anyway to the server if it’s in a localscript so i suggest using remote events.

here’s what i mean

That’s why you should use remote events.

2 Likes

Ahh, I completley forgot about Clients and Servers. That’s why it won’t work. I’ll have to change the local script to a Server script in order to accomplish such. If no success with that I’ll try remote events. Thanks man for that example as well!

That isn’t possible from the client (from a LocalScript). The script is never un-disabling because the client doesn’t have permission to do that.

A fix that respects FilteringEnabled:
Make a RemoteEvent in the tool.
Make the local script fire it one way when it swings the sword:

-- somewhere outside the function...
local event = script.Parent.RemoteEvent

-- ...
	if canSwing then
		canSwing = false
		track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
		track.Priority = Enum.AnimationPriority.Action
		-- DamageScript.Disabled = false -- does not work!
		event:FireServer() -- tell the server you've swung
		track:Play()
		wait(1)
		-- DamageScript.Disabled = true -- does not work
		canSwing = true

Then make the sword only damage when it’s been told to do that, within the last 1-2 seconds.

--Server Script
local lastSwing = 0 -- when was the sword last swung?

function onSwing(player)
	-- TODO: verify that the player is the one holding the sword, so randoms can't make your sword start damaging
	lastSwing = os.clock()
end

function onTouched(hit)
	-- was the sword used within the last second?
	if os.clock() - lastSwing <= 1 then
		local human = hit.Parent:findFirstChild("Humanoid")
		if (human ~= nil) then
			human:TakeDamage(8.5)
		end
	end
end
script.Parent.Handle.Touched:connect(onTouched)
script.Parent.RemoteEvent.OnServerEvent:connect(onSwing)

You can also use the remote to set whether the sword will damage or not, if you feel up to it, but I’m leaving it up to you to figure it out
(Note: I don’t know whether os.clock() works in ROBLOX. You can use another way to time it, such as workspace.DistributedGameTime or something)

1 Like

The point of using a LocalScript is that you can make the animation play as soon as the player uses the sword. If you simply change it to a Script, then the sword will have some lag between clicking and swinging.

1 Like

Thank you for this skeleton! I’ll definetley be refering to this. Thanks again! :heart:

The event isn’t firing and nothing is happening, even when I’ve added print statements they aren’t running. Is it because I am putting the script in the wrong place?
Here is how it’s looking in the explorer window

https://gyazo.com/b577795a23c488292e1e0d954d06a382

If your Script is doing nothing, even when it’s got a print() at the very top, then it may well be disabled; re-enable it.

Anything else I can think of, should be making an error by now

2 Likes