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

How do i make it so my hand deals damage only when i press F button?

Asked by 3 years ago
Edited 3 years ago

SCRIPT :

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 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

    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
    end)

    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
    end)


end)

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

LocalScript :

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)

Basically, when i go next to a play and my hand touches his body it deals damage anyway, and i want it to deal damage only when i press a certain key. Please teach me, i don t want a script i want an explanation so i can understand it and learn it for further projects !

0
what did it do in reality, not expectations Xapelize 2658 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You should disconnect the Touched event when its called so that it doesn't fire the function until you press F again

local connectionRight, connectionLeft

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
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
end)

wait(1) --wait until the animation is done
connectionRight:Disconnect()
connectionLeft:Disconnect()
0
Thank you so much , i learned a new thing from you called Disconnect(), more knowledge on the way :D Hate_ZEU 47 — 3y
0
quick note, you should disconnect the events outside of the function because the event wouldnt disconnect until it hits something and thats not what you want, I updated the script :3   mixgingengerina10 223 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Scripting helpers is NOT a request site! Your question will soon be seen and deleted by a moderator. Please read the community guidelines: https://scriptinghelpers.org/help/community-guidelines

0
What do you mean, i didn`t request, i only asked what should i do to make this work, i even typed down i don`t want an entire script just an explanation Hate_ZEU 47 — 3y
0
This site is for solving errors lady JasiuJasiuB 13 — 3y
0
im a moderator and im banning you before him Xapelize 2658 — 3y
0
joke Xapelize 2658 — 3y
View all comments (2 more)
0
but he made his own script and thats weird he wanted explanation Xapelize 2658 — 3y
0
First of all jasiu don`t call me lady you don`t even know me, and xapelize i only asked for a help because i don`t know what to do to make it so it deals damage only when i use a certain key , my script deals damage when my hands touches the character even tho i don`t press on F and that`s what i want a fix for, don`t be toxic Hate_ZEU 47 — 3y

Answer this question