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

Why is the enemy getting damage without punching?

I have a combat system in my game and everything works fine, but there is one thing thats still bad,the enemy gets damage just by touching.Here are the scripts (local and server): Please i need help with that

Local:

local UserInputService = game:GetService("UserInputService")
local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char.Humanoid
local Hit = script:WaitForChild('Hit')
local CoolDown = 1
local Re = game:GetService("ReplicatedStorage").Combat
local Punch = game:GetService("ReplicatedStorage").PunchR15
local PunchR152 = game:GetService("ReplicatedStorage").PunchR152
local Punch2 = game:GetService("ReplicatedStorage").PunchR6
local Kick = game:GetService("ReplicatedStorage").KickR15
local Debounce = true
local Key = 'Q'
local ContextActionService = game:GetService("ContextActionService")
UserInputService.InputBegan:Connect(function(input, Istyping)
	if Istyping then return end
	if input.KeyCode == Enum.KeyCode[Key] and Debounce and Char then
		Debounce = false
		if script.Combo.Value == 1 then
		print(script.Combo.Value)
		script.Combo.Value = 2
		local Load = Humanoid:LoadAnimation(Punch,Punch2)
		Load:Play()
		local damage = script.Damage.Value
		Re:FireServer(damage)
	
		elseif script.Combo.Value == 2 then
			print(script.Combo.Value)
			script.Combo.Value = 3
			local Load = Humanoid:LoadAnimation(PunchR152)
		    Load:Play()
			local damage = script.Damage.Value
			Re:FireServer(damage)
		
		elseif script.Combo.Value == 3 then
			print(script.Combo.Value)
			script.Combo.Value = 1
			local Load = Humanoid:LoadAnimation(Kick)
		    Load:Play()
			local damage = script.Damage.Value
			Re:FireServer(damage)
	
	end
	wait(CoolDown)
		Debounce = true
	end
end)

ServerScript:

local Combat = game:GetService("ReplicatedStorage").Combat
Combat.OnServerEvent:Connect(function(plr,damage)
	local Char = plr.Character or plr.CharacterAdded:Wait()
	if not Char.Keybind.Hit.Value then
		 Char.Keybind.Hit.Value = true
		local A = true
		Char.RightHand.Touched:Connect(function(h)
			if A and h.Parent.Name ~= plr.Name and h.Parent:FindFirstChild('Humanoid')and not h.Parent:FindFirstChild("AlreadyHit "..plr.Name) then
				A = false
				local AlreadyHit = Instance.new("BoolValue")
				AlreadyHit.Name = "AlreadyHit "..plr.Name
				AlreadyHit.Parent = h.Parent
				game.Debris:AddItem(AlreadyHit,1)
				local E = h.Parent.Humanoid
				local s = script.Sound:Clone()
				s.Parent = h
				s:Play()
				E:TakeDamage(damage)
				print("take"..damage)
				wait(3)
				s:Remove()
				A = true
			end
		end)
	wait()
	Char.Keybind.Hit.Value = false
	
	end
end)

Thank you!

4 comments

0

Trying changing the and not h.Parent:FindFirstChild("AlreadyHit " .. plr.Name) to and h.Parent:FindFirstChild("AlreadyHit " .. plr.Name) == nil then

0

Shouldn’t the enemy get damaged if the players arm touches the enemy? What do you mean just by touching?

0

Ohhh I know what you mean.

0

Keep A false after the touches event is done. Don’t set it back to true cause then the touches event will run event if it’s in the if statement.

1 answer

✓ Accepted answer

local Combat = game:GetService("ReplicatedStorage").Combat
Combat.OnServerEvent:Connect(function(plr,damage)
    local Char = plr.Character or plr.CharacterAdded:Wait()
    local A = false
    if not Char.Keybind.Hit.Value then
         Char.Keybind.Hit.Value = true
        A = true
        Char.RightHand.Touched:Connect(function(h)
            if A and h.Parent.Name ~= plr.Name and h.Parent:FindFirstChild('Humanoid')and not h.Parent:FindFirstChild("AlreadyHit "..plr.Name) then
                A = false
                local AlreadyHit = Instance.new("BoolValue")
                AlreadyHit.Name = "AlreadyHit "..plr.Name
                AlreadyHit.Parent = h.Parent
                game.Debris:AddItem(AlreadyHit,1)
                local E = h.Parent.Humanoid
                local s = script.Sound:Clone()
                s.Parent = h
                s:Play()
                E:TakeDamage(damage)
                print("take"..damage)
                wait(3)
                s:Remove()
            end
        end)
    wait()
    Char.Keybind.Hit.Value = false

    end
end)

What was happening was A = true was causing the touched event to run after it ran the first time.if you put a listener inside an if statement it still runs even if the if statement isn’t satisfied. So since the if statement wasn’t satisfied and A was true the if statement inside the touched event that checked A ran cause A was true. If that makes sense

1 comment

0

You should declare A outside the if statement as false too. Cause it will try to check A which doesn’t exist. So local A = false before everything

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