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

[SOLVED] Punching script does damage whether or not the user is currently punching?

So I'm trying to make a punching script that works with filtering enabled because of course, all my problems these days lie in FE. So, I made a script to where when you press e, it does a punch, it cycles through 4 different animations, and uses 4 different body parts with a touched event to do damage. It does damage, although I feel like it does a lot more than it should. After I punch, whenever someone touches the body part that did the punch, they get hurt, even after I'm done punching. I got it to work fine without FE, but now that I'm trying to use remote events and whatever, it's bugging out.

Server:

local punchingEvent = game.ReplicatedStorage:WaitForChild("Punching")

local punch1wait = 0.5
local punch2wait = 0.5
local punch3wait = 0.5
local punch4wait = 0.5

punchingEvent.OnServerEvent:connect(function(player,bodyPart,punching,punchNumber)
	local char = game.Workspace:WaitForChild(player.Name)
	
	local stats = player:WaitForChild("Stats")
	
	local strengthf = stats:WaitForChild("Strength")
	local agilf = stats:WaitForChild("Agility")
	local jumpf = stats:WaitForChild("Jump")
	
	local strength = strengthf:WaitForChild("StrengthLevel")
	local agil = agilf:WaitForChild("AgilityLevel")
	local jump = jumpf:WaitForChild("JumpLevel")
	
	local dmg = true
	local db = false
	
	char:WaitForChild(bodyPart).Touched:connect(function(hit)
		if dmg == true and db == false then
			db = true
			hit.Parent.Humanoid:TakeDamage(strength.Value + math.random(10,15))
			dmg = false
			if punchNumber == 1 then
				wait(punch1wait)
			elseif punchNumber == 2 then
				wait(punch2wait)
			elseif punchNumber == 3 then
				wait(punch3wait)
			elseif punchNumber == 4 then
				wait(punch4wait)
			end
			dmg = true
			db = false
		end
	end)
end)

Client:

local player = game.Players.LocalPlayer
local char = player.Character
local stats = player:WaitForChild("Stats")
local leaderstats = player:WaitForChild("leaderstats")
local mouse = player:GetMouse()
local inmotion = false
local multiplier = char.Humanoid:WaitForChild("XP Multiplier")
local running = script:WaitForChild("Running")
local damage = false
local db = false
local remoteEvent = game.ReplicatedStorage.UpStats
local punchingRemoteEvent = game.ReplicatedStorage:WaitForChild("Punching")

local strength = stats:WaitForChild("Strength")
local strengthlvl = strength:WaitForChild("StrengthLevel")
local sxp = strength:WaitForChild("StrengthXp")
local smax = strength:WaitForChild("StrengthMaxXp")
local pushwaittime = 3

local agility = stats:WaitForChild("Agility")
local agillvl = agility:WaitForChild("AgilityLevel")
local axp = agility:WaitForChild("AgilXp")
local amax = agility:WaitForChild("AgilMaxXp")
local lungewaittime = 2.2

local jump = stats:WaitForChild("Jump")
local jumplvl = jump:WaitForChild("JumpLevel")
local jxp = jump:WaitForChild("JumpXp")
local jmax = jump:WaitForChild("JumpMaxXp")
local squatwaittime = 1.8

local stamStuff = player:WaitForChild("StaminaStuff")
local stamina = stamStuff:WaitForChild("Stamina")
local maxStam = stamStuff:WaitForChild("MaxStamina")

local squatanim = "rbxassetid://2219181231"
local lungeanim = "rbxassetid://2380575156"
local pushanim = "rbxassetid://2219303213"
local punch1 = "rbxassetid://2219184536"
local punch2 = "rbxassetid://2219185342"
local punch3 = "rbxassetid://2219186257"
local punch4 = "rbxassetid://2219189003"
local runAnimId = "rbxassetid://2394625968" 

local punch1wait = 0.5
local punch2wait = 0.5
local punch3wait = 0.5
local punch4wait = 0.5

-- Squat
mouse.KeyDown:connect(function(key)
	if key == "c" then
		if inmotion == false and db == false then
			inmotion = true
			db = true
			local anim = Instance.new("Animation")
			anim.AnimationId = squatanim
			local squat = char.Humanoid:LoadAnimation(anim)
			squat.Priority = Enum.AnimationPriority.Action
			squat:Play()
			wait(squatwaittime)
			remoteEvent:FireServer("jump")
			squat:Stop()
			inmotion = false
			db = false
		end
	end
end)

-- Lunge
mouse.KeyDown:connect(function(key)
	if key == "x" then
		if inmotion == false and db == false then
			inmotion = true
			db = true
			local anim = Instance.new("Animation")
			anim.AnimationId = lungeanim
			local lunge = char.Humanoid:LoadAnimation(anim)
			lunge.Priority = Enum.AnimationPriority.Action
			lunge:Play()
			wait(lungewaittime)
			remoteEvent:FireServer("agility")
			lunge:stop()
			inmotion = false
			db = false
		end
	end
end)

-- Pushup
mouse.KeyDown:connect(function(key)
	if key == "z" then
		if inmotion == false and db == false then
			inmotion = true
			db = true
			local anim = Instance.new("Animation")
			anim.AnimationId = pushanim
			local pushanim = char.Humanoid:LoadAnimation(anim)
			pushanim.Priority = Enum.AnimationPriority.Action
			pushanim:Play()
			wait(pushwaittime)
			pushanim:Stop()
			remoteEvent:FireServer("strength")
			inmotion = false
			db = false
		end
	end
end)

-- Sprint
mouse.KeyDown:connect(function(key)
	if key:byte() == 50 then
		if running.Value == false then
			if inmotion == false and stamina.Value >= 0 then
				local anim = Instance.new("Animation")
				anim.AnimationId = runAnimId
				runAnim = char.Humanoid:LoadAnimation(anim)
				runAnim.Priority = Enum.AnimationPriority.Action
				inmotion = true
				running.Value = true
				runAnim:Play()
				runAnim:AdjustSpeed((agillvl.Value*0.003+1.1))
				char.Humanoid.WalkSpeed = agillvl.Value * 0.3 + 25
				char.Humanoid.JumpPower = jumplvl.Value * 0.5 + 60
				while running.Value == true and wait(0.1) do
					if stamina.Value > 0 then
						stamina.Value = stamina.Value - 1
					elseif stamina.Value <= 0 then
						inmotion = false
						running.Value = false
						runAnim:Stop()
						char.Humanoid.WalkSpeed = 16
						char.Humanoid.JumpPower = 50
						break
					end
				end
			end
		elseif running.Value == true then
			inmotion = false
			running.Value = false
			runAnim:Stop()
			char.Humanoid.WalkSpeed = 16
			char.Humanoid.JumpPower = 50
		end
	end
end)

-- Punching
local punching = false
local count = 0
mouse.KeyDown:connect(function(key)
	if key == "e" and damage == false then
			local anim = Instance.new("Animation")
			if db == false and damage == false then
				if count == 0 then
					db = true
					anim.AnimationId = punch1
					local punchanim = char.Humanoid:LoadAnimation(anim)
					punchanim.Priority = Enum.AnimationPriority.Action
					punchanim:Play()
					punchingRemoteEvent:FireServer("LeftHand",punching,1)
					damage = true
					wait(punch1wait)
					damage = false
					punchanim:Stop()
					count = 1
					db = false
					punching = false
				elseif count == 1 then
					db = true
					anim.AnimationId = punch2
					local punchanim = char.Humanoid:LoadAnimation(anim)
					punchanim.Priority = Enum.AnimationPriority.Action
					punchanim:Play()
					punchingRemoteEvent:FireServer("RightHand",punching,2)
					damage = true
					wait(punch2wait)
					damage = false
					punchanim:Stop()
					count = 2
					db = false
					punching = false
				elseif count == 2 then
					db = true
					anim.AnimationId = punch3
					local punchanim = char.Humanoid:LoadAnimation(anim)
					punchanim.Priority = Enum.AnimationPriority.Action
					punchanim:Play()
					punchingRemoteEvent:FireServer("LeftFoot",punching,3)
					damage = true
					wait(punch3wait)
					damage = false
					punchanim:Stop()
					count = 3
					db = false
					punching = false
				elseif count == 3 then
					db = true
					anim.AnimationId = punch4
					local punchanim = char.Humanoid:LoadAnimation(anim)
					punchanim.Priority = Enum.AnimationPriority.Action
					punchanim:Play()
					punchingRemoteEvent:FireServer("RightFoot",punching,4)
					damage = true
					wait(punch4wait)
					damage = false
					punchanim:Stop()
					count = 0
					db = false
					punching = false
				end
			end
			punching = false
	end
end)

11 comments

0

You can send all script of client?

0

Yeah I can, but I doubt it would help you much. It's like 250 lines of code, unless you want to look through all of that.

0

i like to see all for fix~.

0

Is there really anything there to stop the Touched event? The only thing I see that could prevent damage is your debounce, but ultimately it would be best to Disconnect your Touched event

0

yHastee, I told you in the question that the rest of the script was unrelated, but okay.

0

There's not necessarily something to stop it now that I think about it. I guess I could just use a break?

0

How would I disconnect the touched event?

0

you would make line 08 a variable...then when you wanted to stop the damage, you would do varName:Disconnect()

0

yHasteeD what do you mean make line 8 a variable, how would I do that?

0

someVar = punchingEvent.OnServerEvent:Connect() --> someVar:Disconnect()

0

Ah, mk.

1 answer

✓ Accepted answer

Your script does not contain any errors, however you have literally looped the server.

Whenever you press it will change the variable to be able to attack, then you do not need to after touching the other player will set dmg = true db = false because in your script already it changes. If set this with touch player, start a loop

Also your waits after punching help give double damage, so I recommend you take it out.

Code tested in FE:

SERVER

local punchingEvent = game.ReplicatedStorage:WaitForChild("Punching")

punchingEvent.OnServerEvent:connect(function(player,bodyPart,punching,punchNumber)
    local char = game.Workspace:WaitForChild(player.Name)

    local stats = player:WaitForChild("Stats")

    local strengthf = stats:WaitForChild("Strength")
    local agilf = stats:WaitForChild("Agility")
    local jumpf = stats:WaitForChild("Jump")

    local strength = strengthf:WaitForChild("StrengthLevel")
    local agil = agilf:WaitForChild("AgilityLevel")
    local jump = jumpf:WaitForChild("JumpLevel")

    local dmg = true
    local db = false

    char:WaitForChild(bodyPart).Touched:connect(function(hit)
        if dmg == true and db == false then
            db = true
            hit.Parent.Humanoid:TakeDamage(strength.Value + math.random(10,15))
            dmg = false
        end
	wait(0.05)
    end)
end)

Errors? tell-me on comments.

3 comments

0

I assume you are turning dmg to false to prevent all future damage? If so, it would be better to entirely Disconnect the Touched event as I mentioned in the comments

0

By triggering the event again it will be able to deal damage. Even because every time tight And the event is triggered so much so.

0

I think it works, thanks!

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