Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 !

1 comment

0

what did it do in reality, not expectations

2 answers

✓ Accepted answer

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

2 comments

0

Thank you so much , i learned a new thing from you called Disconnect(), more knowledge on the way :D

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  

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

6 comments

0

What do you mean, i didnt request, i only asked what should i do to make this work, i even typed down i dont want an entire script just an explanation

0

This site is for solving errors lady

0

im a moderator and im banning you before him

0

joke

0

but he made his own script and thats weird he wanted explanation

0

First of all jasiu dont call me lady you dont even know me, and xapelize i only asked for a help because i dont 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 dont press on F and thats what i want a fix for, dont be toxic

This discussion is preserved as it was. New answers, comments, and votes are closed.