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

Help my punch script isnt working and it makes me take damage how do i fix this?

whenever the player presses Q to punch it damages the player what do i do?


local user = game:GetService("UserInputService")
local play = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character
local target = player:FindFirstChild("Humanoid")
local person = character:WaitForChild("Humanoid") 
local anim = Instance.new("Animation")
local pain = true
anim.AnimationId = "rbxassetid://2642258675"
local key = Enum.KeyCode.Q
local function IsQkeydown()
	return user:IsKeyDown(key)
	end


local function Input(input, gameProcessedEvent)
	if not IsQkeydown() then
	else
	
		print("hit")
		local AnimationTrack = person:LoadAnimation (anim)
		AnimationTrack:Play()
		if target.Touched:Connect(function(hit)
	target:TakeDamage(5)
		 
		
	 end)

			
			
	then	end 
		
			end
		end
		
 
user.InputBegan:Connect(Input)



2 comments

0

line 23: target.Touched is an event, it does not return a bool but returns a signal where you can connect function listeners

0

How do i do that?

1 answer

My question is... How on Earth did any of that code even cause damage!?

You're trying to define the Humanoid through the LocalPlayer, this severely bewilders me!

But, in some twisted universe, you managed to get it get the humanoid of the very player, that's why it's hurting the player.

I recommend you remove the "if" from:

if target.Touched:Connect(function(hit)

would be:

target.Touched:Connect(function(hit)

As GoldAngelInDisguise was hinting towards. I recommend creating a hit function like this (and defining Humanoid properly!):

local hmd = character:WaitForChild("Humanoid",5)

local db = false -- debounce (prevents the function from being fired a million times
hmd.Touched:connect(function(hit)
	if db == false then
		db = true
		if hit.Parent:IsA("Model") then -- Checks if target is a model
			if hit.Parent:FindFirstChildOfClass("Humanoid") then -- Checks if the hit object has a humanoid
				target = hit.Parent:FindFirstChildOfClass("Humanoid")
				target:TakeDamage(5)
			end
		end
	end
	wait(0.4)
	db = false
end)

Just how your script didn't even stop working confuses me to the deepest parts of my brain. But I'll just proclaim you're using black Roblox magic and give you that script excerpt to help you during your journey.

4 comments

1

connect is deprecated, use Connect

0

wait what, I thought it was the other way around

0

I thought "Connect" was the one that got deprecated lol

0

It still isnt working and im still taking damage from it

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