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

Why is my attack script not doing damage? The damage script normally works in other situations.

Asked by 4 years ago

I have created a script that clones a part called "Damager" onto a hitbox. Damager should damage everything it touches, however for some reason it is not dealing damage when it is cloned. This is the script in damager:

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 2 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)

It should work. This next script is the script that makes the attack work:

 local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()



local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=4752814788"

mouse.KeyDown:connect(function(key)
  if key == "e" then
    if script.Cooldown.Value == false then
    script.Cooldown.Value = true
   local playAnim = humanoid:LoadAnimation(anim)
   playAnim:Play()

    local Damage1 = workspace.Clonables.Damager:Clone()
    Damage1.Parent = workspace
    Damage1.Position = player.Character.Hitbox1.Position
    Damage1.Orientation = player.Character.Hitbox1.Orientation
    local weld1 = Instance.new("WeldConstraint")
    weld1.Parent = Damage1
    weld1.Enabled = true
    weld1.Part0 = Damage1
    weld1.Part1 = player.Character.Hitbox1



    local Damage2 = workspace.Clonables.Damager:Clone()
    Damage2.Parent = workspace
    Damage2.Position = player.Character.Hitbox2.Position
    Damage2.Orientation = player.Character.Hitbox2.Orientation
    local weld2 = Instance.new("WeldConstraint")
    weld2.Parent = Damage2
    weld2.Enabled = true
    weld2.Part0 = Damage2
    weld2.Part1 = player.Character.Hitbox2


    wait (0.4)
    script.Cooldown.Value = false
    weld1:Destroy()
    weld2:Destroy()
    Damage1:Destroy()
    Damage2:Destroy()

    end
    end
end)

In theory, all of this should work perfectly, but for some reason it is not dealing damage. Does anyone know why? Please help.

0
I think the problem is at line 12, don’t think keyboard input doesn’t work like that. TTHKKB 12 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Welp, I actually dont know what is the error on your script, but i got this one that i used in my old game and it still working

 --// Services
local UserInputService = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Players = game:GetService('Players')

--// Debounces
local Punching = false
local Damaging = true

--// Customisation
local Damage = 20
local Cooldown = 0.5

--// Events
local PunchEvent = ReplicatedStorage:WaitForChild('PunchEvent')

--// Player
local Player = game.Players.LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
local Humanoid = Character:FindFirstChildOfClass('Humanoid')
local RightArm = Character:WaitForChild('Right Arm')

--//Animations
Punch = Instance.new('Animation')
Punch.AnimationId = 'rbxassetid:// '--animation here
PunchTrack = Humanoid:LoadAnimation(Punch)

--// Coding
UserInputService.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.R and not Punching then
        Punching = true
        PunchTrack:Play()
        PunchTrack.KeyframeReached:Connect(function(Keyframe)
            if Keyframe == 'End' then
                wait(Cooldown)
                Punching = false
                Damaging = true
            end
        end)
    end
end)

RightArm.Touched:Connect(function(hitPart)
    if Punching and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then
        Damaging = false
        local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid')
        PunchEvent:FireServer(HumanoidToDamage, Damage)
    end
end)

ALSO.... add a RemoteEvent called "PunchEvent" (its really important that u name it PunchEvent cuz if u dont it wont work. Make this correct answer if it is

0
U can customize it as u want to Shadic1270 136 — 4y
Ad
Log in to vote
0
Answered by
Dfzoz 489 Moderation Voter
4 years ago

Is the damager script disabled? You have to turn disabled to false on the clone of the hitbox you make

0
I added lines to make sure Disabled is off, and it still doesn't do damage. Thanks anyways, though. luckylucas9 0 — 4y

Answer this question