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

How do i fix this punching system ?

Asked by 3 years ago
Edited 3 years ago

Hello, i have a script that deals damage whenever i hit F, like throws a punch and deals damage, the problem is that if there are 2 or more players, if 1 player presses F and punches, the other players can`t, how can i fix it ?

Local script in starterPlayerScripts :

local UIS = game:GetService('UserInputService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local remotePunch = replicatedStorage:WaitForChild('Punch')

UIS.InputBegan:connect(function(input, isTyping)
    if isTyping then return end
    if input.KeyCode == Enum.KeyCode.F then
        remotePunch:FireServer()
        end
end)

Script for system in serverscriptservice :

local replicatedStorage = game:GetService('ReplicatedStorage')
local PunchingRemote = replicatedStorage:WaitForChild('Punch')
local anim1 = Instance.new('Animation')
local anim2 = Instance.new('Animation')
anim1.AnimationId = 'http://www.roblox.com/asset/?id=6985138442'
anim2.AnimationId = 'http://www.roblox.com/asset/?id=6984519609'
local var = 1
local debounce = false
local canDoDmg = true
PunchingRemote.OnServerEvent:connect(function(plr)
    local connectionRight, connectionLeft
    local track1 = plr.Character.Humanoid:LoadAnimation(anim1)
    local track2 = plr.Character.Humanoid:LoadAnimation(anim2)
    local character = plr.Character
    local humanoid = character:WaitForChild('Humanoid')
    if var == 1 and debounce == false then
        debounce = true
        plr.leaderstats.Effort.Value = plr.leaderstats.Effort.Value + plr.Stats.Earnings.Value
        track1:Play()
        var = var + 1
        wait(1)
        debounce = false
    elseif var == 2 and debounce == false then 
        debounce = true
        plr.leaderstats.Effort.Value = plr.leaderstats.Effort.Value + plr.Stats.Earnings.Value
        track2:Play()
        var = 1
        wait(1)
        debounce = false
    end

    connectionRight = plr.Character.RightHand.Touched:connect(function(hit)
        if hit.Parent.Humanoid and canDoDmg then 
            hit.Parent.Humanoid:TakeDamage(plr.Stats.Damage.Value)
            canDoDmg = false
            wait(1)
            canDoDmg = true
        end
        connectionRight:Disconnect()

    end)

    connectionLeft = plr.Character.LeftHand.Touched:connect(function(hit)
        if hit.Parent.Humanoid and canDoDmg then 
            hit.Parent.Humanoid:TakeDamage(plr.Stats.Damage.Value)
            canDoDmg = false
            wait(1)
            canDoDmg = true
        end
        connectionLeft:Disconnect()
    end)


end)

while wait(4) do
    if var == 2 then
        var = 1
    end
end



Please help, idk what to do, i`ll mark answer.

1 answer

Log in to vote
0
Answered by
jwklong 32
3 years ago

its because var is stored inside the server script. if one player presses F it goes on cooldown for both players, i suggest using leaderstats so it can have cooldowns for both players without stopping the other.

0
Thank you so much, i was thinking this was the problem but i wanted to know for sure :D, you are the best ! Hate_ZEU 47 — 3y
0
no problem jwklong 32 — 3y
Ad

Answer this question