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

My punch script spams FireServer() and only does 1 type of punch?

Asked by 6 years ago
Edited 6 years ago

I made a punch script and for some odd reason, with no errors, the punch script only does a Left Punch, and it's spammable if I didn't put in the cooldown in the local script. Here's the server sided.

local PunchEvent = game.ReplicatedStorage.punch
local Cooldown = 0.4

local PunchData = {}

local function PlaySound(ID,Parent)
    local Sound = Instance.new("Sound")
    Sound.SoundId = "rbxassetid://"..ID
    Sound.PlayOnRemove = true
    Sound.Parent = Parent
    Sound:Destroy()
end

local torw = 711753382

PunchEvent.OnServerEvent:Connect(function(Player)
    local Character = Player.Character  

    while Character.Parent == nil do
        wait()
    end

    local Humanoid = Character:WaitForChild("Humanoid")
    local Animator = Humanoid:WaitForChild("Animator")

    PunchData[Player] = {
        ["LeftPunch"] = Animator:LoadAnimation(script.Left),
        ["RightPunch"] = Animator:LoadAnimation(script.Right),
        ["LastPunch"] = "LeftPunch",
        ["CanPunch"] = true
    }

    local SecondaryDataTable = PunchData[Player]

    local RightHand = Character.RightHand
    local LeftHand = Character.LeftHand

    local CurrentConnection = nil
    local CurrentAnimationTrack = nil

    if SecondaryDataTable.CanPunch then
        if SecondaryDataTable.LastPunch == "LeftPunch" then
            SecondaryDataTable.CanPunch = false
            PlaySound(711753382, Character.RightHand)
            CurrentAnimationTrack = SecondaryDataTable.RightPunch

            CurrentConnection = Character.RightHand.Touched:Connect(function(Hit)
                local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
                if Humanoid then
                Humanoid:TakeDamage(12)
                PlaySound(367499850, RightHand)
                CurrentConnection:Disconnect()
                end
            end)

            SecondaryDataTable.LastPunch = "RightPunch"

        elseif SecondaryDataTable.LastPunch == "RightPunch" then
            SecondaryDataTable.CanPunch = false
            PlaySound(711753382, LeftHand)
            CurrentAnimationTrack = SecondaryDataTable.LeftPunch

            CurrentConnection = Character.LeftHand.Touched:Connect(function(Hit)
                local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
                if Humanoid then
                Humanoid:TakeDamage(12)
                PlaySound(367499850, LeftHand)
                CurrentConnection:Disconnect()
                end
            end)

            SecondaryDataTable.LastPunch = "LeftPunch"

        end
        CurrentAnimationTrack:Play()
        CurrentAnimationTrack.Stopped:Wait()
        if CurrentConnection then CurrentConnection:Disconnect() end

        wait(Cooldown)
        SecondaryDataTable.CanPunch = true
    end
end)

And here's the local script.

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character

local Mouse = Player:GetMouse()
local PunchEvent = game.ReplicatedStorage.punch

local TakeStamina = game.ReplicatedStorage.TakeStamina

local Data = Player.Data
local Stamina = Data.Stamina

local Debounce = false

Mouse.Button1Down:Connect(function()
    if Stamina.Value > 11 then
        if not Debounce then
        Debounce = true
        PunchEvent:FireServer()
        TakeStamina:FireServer(10)
        wait(0.4)
        Debounce = false
        end
    end
end)
0
connect is deprecated use Connect User#19524 175 — 6y
0
Edited. FireyMcBlox 134 — 6y

1 answer

Log in to vote
1
Answered by
Faeyll 22
6 years ago

There's a few things going on here.

You're modifying a rescoped local reference to a table rather than the table itself. Thus the higher scope PunchData[Player].LastPunch is always LeftPunch because you never set the new one. The way to fix this would be PunchData[Player].LastPunch = "LeftPunch"

You're also resetting PunchData[Player] every time the event happens. Lines 26-31 shouldn't be fired every time the event happens, rather every time they NEED to be fired. That would be when the tool is first equipped, generally.

You should also notice that the cooldown doesn't occur with this, as it's a reference to a table

Ad

Answer this question