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

Why does my punch script keep doing weird things like stacking everytime i press e and?

Asked by 5 years ago
Edited 5 years ago

every time i press e the damage seems to stack and it seems that even if i don't press e after pressing e just when my right hand touches a humanoid gets damaged it

Heres the local script

local userinputservice = game:GetService("UserInputService")
local Plr = game.Players.LocalPlayer
local char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = char:WaitForChild('Humanoid')

local Hit = script:WaitForChild("Hit")-- a bool value 

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild('CombatFolder'):WaitForChild('RemoteEvent')
local Punch = game.ReplicatedStorage:WaitForChild('CombatFolder'):WaitForChild('Punch') -- Punch is a animation 

local Debounce = true
local CoolDown = 2
local Key = 'E'

userinputservice.InputBegan:Connect(function(input,IsTyping)
    if IsTyping then return end
    if input.KeyCode == Enum.KeyCode[Key] and Debounce and char then
        Debounce = false
        local Load = Humanoid:LoadAnimation(Punch)
        Load:Play()


        if Load.IsPlaying and not Hit.Value then
            RemoteEvent:FireServer()


        end
    wait(CoolDown)
    Debounce = true 
    end


end)

and heres the server script

local Combat = game:GetService("ReplicatedStorage").CombatFolder:WaitForChild('RemoteEvent')

Combat.OnServerEvent:Connect(function(player)
    local char = player.Character
    if not player.Backpack.KeyBind.Hit.Value then
        player.Backpack.KeyBind.Hit.Value = true
        local Bool = true 

        char.RightHand.Touched:Connect(function(h)
            if Bool == true and h.Parent.Name ~= player.Name and h.Parent:FindFirstChild('Humanoid') then
                Bool = false
                local EnemyHumnoid = h.Parent.Humanoid
                EnemyHumnoid:TakeDamage(10)
                wait(1)
                Bool = true

            end

        end)
wait()
    player.Backpack.KeyBind.Hit.Value = false
    end
end)
0
because characters are made of multiple body parts theking48989987 2147 — 5y

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago

In the server script, you're connecting a Touched event to the client's Right Hand every time the Combat remote is fired. To avoid this, simply disconnect the connection:

local Combat = game:GetService("ReplicatedStorage").CombatFolder:WaitForChild('RemoteEvent')

Combat.OnServerEvent:Connect(function(player)
    local char = player.Character
    if not player.Backpack.KeyBind.Hit.Value then
        player.Backpack.KeyBind.Hit.Value = true
       -- local Bool = true - this is unneccessary since the touched connection is being disconnected

    local Collided = false -- this is to check if the right hand collided a part or not. If it didn't the connection would be disconnected after a number of seconds
    local TouchedConnection
       TouchedConnection = char.RightHand.Touched:Connect(function(h)
        TouchedConnection:Disconnect()
        TouchedConnection = nil
        Collided = true 
            if h and h.Parent.Name ~= player.Name and h.Parent:FindFirstChild('Humanoid') then 
                local EnemyHumnoid = h.Parent.Humanoid
                EnemyHumnoid:TakeDamage(10)
            end

        end)

    spawn(function()
        wait(1) -- you can modify this if you want
        if not Collided then 
            TouchedConnection:Disconnect()
            TouchedConnection = nil
        end
    end)

wait()
    player.Backpack.KeyBind.Hit.Value = false
    end
end)
Ad

Answer this question