New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Why is the enemy getting damage without punching?

Asked by 8 days ago

I have a combat system in my game and everything works fine, but there is one thing thats still bad,the enemy gets damage just by touching.Here are the scripts (local and server): Please i need help with that

Local:

local UserInputService = game:GetService("UserInputService")
local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char.Humanoid
local Hit = script:WaitForChild('Hit')
local CoolDown = 1
local Re = game:GetService("ReplicatedStorage").Combat
local Punch = game:GetService("ReplicatedStorage").PunchR15
local PunchR152 = game:GetService("ReplicatedStorage").PunchR152
local Punch2 = game:GetService("ReplicatedStorage").PunchR6
local Kick = game:GetService("ReplicatedStorage").KickR15
local Debounce = true
local Key = 'Q'
local ContextActionService = game:GetService("ContextActionService")
UserInputService.InputBegan:Connect(function(input, Istyping)
    if Istyping then return end
    if input.KeyCode == Enum.KeyCode[Key] and Debounce and Char then
        Debounce = false
        if script.Combo.Value == 1 then
        print(script.Combo.Value)
        script.Combo.Value = 2
        local Load = Humanoid:LoadAnimation(Punch,Punch2)
        Load:Play()
        local damage = script.Damage.Value
        Re:FireServer(damage)

        elseif script.Combo.Value == 2 then
            print(script.Combo.Value)
            script.Combo.Value = 3
            local Load = Humanoid:LoadAnimation(PunchR152)
            Load:Play()
            local damage = script.Damage.Value
            Re:FireServer(damage)

        elseif script.Combo.Value == 3 then
            print(script.Combo.Value)
            script.Combo.Value = 1
            local Load = Humanoid:LoadAnimation(Kick)
            Load:Play()
            local damage = script.Damage.Value
            Re:FireServer(damage)

    end
    wait(CoolDown)
        Debounce = true
    end
end)

ServerScript:

local Combat = game:GetService("ReplicatedStorage").Combat
Combat.OnServerEvent:Connect(function(plr,damage)
    local Char = plr.Character or plr.CharacterAdded:Wait()
    if not Char.Keybind.Hit.Value then
         Char.Keybind.Hit.Value = true
        local A = true
        Char.RightHand.Touched:Connect(function(h)
            if A and h.Parent.Name ~= plr.Name and h.Parent:FindFirstChild('Humanoid')and not h.Parent:FindFirstChild("AlreadyHit "..plr.Name) then
                A = false
                local AlreadyHit = Instance.new("BoolValue")
                AlreadyHit.Name = "AlreadyHit "..plr.Name
                AlreadyHit.Parent = h.Parent
                game.Debris:AddItem(AlreadyHit,1)
                local E = h.Parent.Humanoid
                local s = script.Sound:Clone()
                s.Parent = h
                s:Play()
                E:TakeDamage(damage)
                print("take"..damage)
                wait(3)
                s:Remove()
                A = true
            end
        end)
    wait()
    Char.Keybind.Hit.Value = false

    end
end)

Thank you!

0
Trying changing the and not h.Parent:FindFirstChild("AlreadyHit " .. plr.Name) to and h.Parent:FindFirstChild("AlreadyHit " .. plr.Name) == nil then XviperIink 314 — 8d
0
Shouldn’t the enemy get damaged if the players arm touches the enemy? What do you mean just by touching? SethHeinzman 268 — 8d
0
Ohhh I know what you mean. SethHeinzman 268 — 8d
0
Keep A false after the touches event is done. Don’t set it back to true cause then the touches event will run event if it’s in the if statement. SethHeinzman 268 — 8d

1 answer

Log in to vote
1
Answered by 8 days ago
Edited 8 days ago

local Combat = game:GetService("ReplicatedStorage").Combat Combat.OnServerEvent:Connect(function(plr,damage) local Char = plr.Character or plr.CharacterAdded:Wait() local A = false if not Char.Keybind.Hit.Value then Char.Keybind.Hit.Value = true A = true Char.RightHand.Touched:Connect(function(h) if A and h.Parent.Name ~= plr.Name and h.Parent:FindFirstChild('Humanoid')and not h.Parent:FindFirstChild("AlreadyHit "..plr.Name) then A = false local AlreadyHit = Instance.new("BoolValue") AlreadyHit.Name = "AlreadyHit "..plr.Name AlreadyHit.Parent = h.Parent game.Debris:AddItem(AlreadyHit,1) local E = h.Parent.Humanoid local s = script.Sound:Clone() s.Parent = h s:Play() E:TakeDamage(damage) print("take"..damage) wait(3) s:Remove() end end) wait() Char.Keybind.Hit.Value = false end end)

What was happening was A = true was causing the touched event to run after it ran the first time.if you put a listener inside an if statement it still runs even if the if statement isn’t satisfied. So since the if statement wasn’t satisfied and A was true the if statement inside the touched event that checked A ran cause A was true. If that makes sense

0
You should declare A outside the if statement as false too. Cause it will try to check A which doesn’t exist. So local A = false before everything SethHeinzman 268 — 8d
Ad

Answer this question