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

Create a punch tool?

So i already have a basic outline with two scripts, a local script for animation and a server script for damage. But its not working. Can anyone help?????

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local isAttacking = false
local idleAnim = humanoid:LoadAnimation(tool:WaitForChild("Idle"))
local punchAnim = humanoid:LoadAnimation(tool:WaitForChild("Punch"))

tool.Equipped:Connect(function()
	idleAnim:Play()
end)

tool.Activated:Connect(function()
	print("Activated!")
	if isAttacking == false then
		isAttacking = true
		local rightArm = tool.Parent:WaitForChild("Right Arm")
		punchAnim:Play()
		rightArm.Touched:Connect(function(hit)
			print("Collided!")
			local humanoid = hit.Parent:WaitForChild("Humanoid")
			if humanoid then
				tool.TouchedEvent:FireServer(humanoid,hit)
			end
		end)
		wait(2)
		isAttacking = false
	end
end)

tool.Unequipped:Connect(function()
	idleAnim:Stop()
end)

There, that's the local script the root of my problem (probably).

3 comments

0

Not a good scripter but I do know if you want to make it so a Client can look like he's punching try using remote events to fire servers https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

0

well i am using remote event

0

which prints work?

1 answer

  • Nothing is avoiding the hand to touch on the lower arm and make the character do damage to itself.

  • Sometimes touched event gives a list of parts that were touched almost together, so if the hand touches multiple body parts at once, it will give damage for every body part.

  • You are not disconnecting your touched event after use, everytime you activate the tool it will create a new connection to the touch and multiple firings will start to happen the more you activate the tool till the game crashes.

  • The Wait() function should be called inside a new thread or it can stop the rest of the script to run while it is waiting. Instead of using wait(), try delay() or put it inside a spawn(). And after you use this you will have to create a debounce sistem for the touch, like a cooldown.

  • Looks like you did not played the punch animation

4 comments

0

im using r6... also what do you mean by disconnecting?

0

oh, fixed it... kinda. it deals damage, but it repeats the damage without waiting, which is an issue. but also it detects the touch even when the tool isn't activated. help?

0

So, you must deactivate the connection after the animation stops or after you hit someone. You need to assign a variable to the connection, like local Variable = Part.Touched:Connect() || Variable:disconnect()

0

I advise to create a function to disconnect from outside, because you cannot call :disconnect from the own connection.

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