Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why Wont this sword script do damage?

Asked by 8 years ago
Edited by M39a9am3R 8 years ago
Player = script.Parent.Parent
c = Player.Character
Sword = Instance.new("Part")
Sword.Parent = c
SwordMesh = Instance.new("SpecialMesh")
SwordMesh.MeshId = "rbxassetid://432068544"
SwordMesh.Parent = Sword
Sword.CanCollide = false

w1 = Instance.new("Weld", c)
w1.Part0 = w1.Parent["Right Arm"]
w1.Part1 = Sword
w1.C1 = CFrame.fromEulerAnglesXYZ(1.5,0,1.4) *CFrame.new(0,1.2,2)
function onSliced(Sword)
    if Sword:FindFirstChild("Torso") then
        Sword.Humanoid.Health = Sword.Humanoid.Health -5.5
        print("Hit")
    end
end

Sword.Touched:connect(onSliced)
--What am I doing wrong When sword touches a torso it should be doing damage but its not..
0
Might want to double check your scripts for inappropriate content before posting. M39a9am3R 3210 — 8y

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

Problem

What is happening is the Touched event provides the part that touched the brick you connected the event to. So on line 14, you're redefining the Sword, when in reality it's the part that is hitting it.

Another issue is you're assuming whatever the sword is hitting is a character. If a part in workspace or in the model you're hitting so happens to have the name Torso, then your script will error.

There may also be that possibility that the sword holder is the person who hit the sword. You can avoid this by making sure that the sword holder is not the thing that hits it.


Solution

On line 14, change Sword to something else. Let's say Target.

We'll also look for Humanoid rather than Torso, and we'll need to check Target's parent for the Humanoid.

If the script were a LocalScript, I do not see why you wouldn't use LocalPlayer. However, since you're code is defining player as script.Parent.Parent I will assume you're using a server script. We'll need to identify if the parent of the part being hit is the player or not. We'll want to get the GetPlayerFromCharacter function and compare it with the player variable.


Final Script

Player = script.Parent.Parent
c = Player.Character
Sword = Instance.new("Part")
Sword.Parent = c
SwordMesh = Instance.new("SpecialMesh")
SwordMesh.MeshId = "rbxassetid://432068544"
SwordMesh.Parent = Sword
Sword.CanCollide = false

w1 = Instance.new("Weld", c)
w1.Part0 = w1.Parent["Right Arm"]
w1.Part1 = Sword
w1.C1 = CFrame.fromEulerAnglesXYZ(1.5,0,1.4) *CFrame.new(0,1.2,2)
function onSliced(Target) --Changed the variable.
    if Target.Parent:FindFirstChild("Humanoid") then --Locating Humanoid.
        if game.Players:GetPlayerFromCharacter(Target.Parent) ~= Player then --Now that we confirmed it was a character model, compare it to if the player is the target.
            Sword.Humanoid.Health = Sword.Humanoid.Health - 5.5 --Reduce health. ForceFields will not respect this method. I would recommend using function TakeDamage().
            print("Hit")
           end
    end
end

Sword.Touched:connect(onSliced)

Hopefully this answered your question. If so, do not forget to hit the accept answer button. If you have any questions, feel free to post them in the comments below.
Ad

Answer this question