Scripting Damage for a sword that comes out of ReplicatedStorage

  1. What do you want to achieve? Well I need help with scripting a sword that comes from ReplicatedStorage and into the backpack of the player.

  2. What is the issue? However when I do this the scripts that are parented to the tool don’t work because I am putting the handle of the sword to the player.

  3. What solutions have you tried so far? I’ve tried figuring it out for the last 45 min.

Code I tried. (In handle[part]).

local Debounce = false

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
        Debounce = true
        hit.Parent.Humanoid:TakeDamage(5)
        wait(1)
        Debounce = false
    end
end)

Also, I saw no error in the output and the script analysis. Help appreciated!

if you need more info, say so!

1 Like

A few things could be going on here.

If you are copying a script and putting it in the tools, make sure that both the code cloning the scripts is not a localscript. If the copying is happening on the client, then the server will not know that the scripts you just made actually exist and won’t be able to run the sword code.

Next, make sure that the scripts that apply the damage are also not localscripts. If the sword scripts that try to deal damage are on the client, the server will filter our the client’s request to damage other players.

It would be helpful if you put print statements at the top of each of the scripts to make sure that they are running. Any information like this could help debug the problem.

An alternative solution: Use a NumberValue object or a IntValue object inside the tools to determine the damage that the sword do. The scripts inside the sword never need to be swapped out, and you can easily change the damage values from the server. Just hook up the sword damage scripts to read the Value object instead of the hardcoded values.

1 Like
        '''
 script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
    game.Workspace.Health.Value = game.Workspace.Health.Value - 5
    game.Workspace.Health.Value = game.Workspace.NPC.Humanoid.Health
    wait(1)
 end

end)
/ ‘’’
I tried this. ( I added a intValue called “Health” in workspace) Doesn’t seen to work. Could you please help me further?

EDIT: I added print statements to the code above and none of the statements printed out.

Anymore suggestions/improvements i need to make?

You have it flipped.

game.Workspace.NPC.Humanoid.Health = workspace.Health.Value --This will only work if it is a Number/Int value.

–VitalWinter brought up a good point, dont use reigon3 for this specifically, but Touched does have faults for length

I reccomend to add a few print statements. (like what VitalWinter said) This will help debugging.

script.Parent.Touched:Connect(function(hit) --connect was deprecated
    print("Sword was touched!")
    if hit.Parent:FindFirstChild("Humanoid") then
        if Debounce == true then return end --Printing reasons
        Debounce = true
        hit.Parent.Humanoid:TakeDamage(5)
        wait(1)
        Debounce = false
     else
        print("Humanoid not found!")
    end
end)

It more than likely is a problem with the model. Tell me anything that prints.

2 Likes

If this is for a sword tool, I’d definitely NOT use Region3 for hit detection. Touched events are perfectly reliable, and there actually are a few caveats with Region3 that don’t make sense for this situation.

1 Like

If a print statement at the top of the script isn’t even printing, then you script isn’t even running. I’d play the game in studio and look in the explorer to see what is going on. You can also use more prints to help find the earliest case where you notice that the system isn’t working as expected.

Again, it might be because you are expecting a localscript to run in a server environment or a server script that is cloned locally not running because the server doesn’t know about it.

To springboard off what VitalWinter said, a couple more ideas:

  1. hit.Parent can be nil. So hit.Parent:FindFirstChild() can error. You would see this in the Output pane.

  2. Learning to use the Roblox Studio debugger is a great use of your time, it will save you lots of time in the future, even outside of Roblox. Here’s a tutorial - Documentation - Roblox Creator Hub

Basically what I would do in this case is set some breakpoints (F9) on a couple lines of your script. I would confirm several things: that the touch event handler is actually being called, that your code finds a Humanoid, and that TakeDamage is actually called. You can do this by setting breakpoints on each of these lines.

Before you try to make cloning a sword out of ReplicatedStorage work, make sure it works just in the Workspace. There’s really nothing special about that step, so I would guess your code is broken even if the sword is just laying on the ground.

3 Likes