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

How do I make my hitbox system more efficient?

I'm making a fighting game, and I want it so players can continue attacking the monster by spam clicking (or hold clicking), but the current system I have isn't working. I have it so when the weapon handle is touched and if the name of the object that touched it is a 'Hitbox' then it will fire the rest of the code. It works but you have to keep moving in order for the touched event to fire otherwise if you stand still and keep clicking it doesn't do any damage.

How do I make this better? This is in a local script, in a server script I just subtract the value from the health that I sent over, it's nothing much. I was thinking maybe checking if the Hitbox was hit instead? Or should I instead check if the player is within range of the hitbox using magnitude and then if they click with the weapon it would fire to the server? I don't know the best approach for this.

local tool = script.Parent
local handle = script.Parent.Handle
local player = game.Players.LocalPlayer

repeat wait() until player.Character

local character = player.Character
local humanoid = character:WaitForChild('Humanoid')

local animation = Instance.new('Animation')
animation.Parent = script.Parent
animation.AnimationId = 'rbxassetid://210638956'

local animTrack = humanoid:LoadAnimation(animation)

local canAttack = true

tool.Activated:Connect(function()
	if canAttack then
		animTrack:Play()
		handle.Touched:Connect(function(hit)
			if hit.Parent ~= character and hit.Name == 'Hitbox' then
				if canAttack and hit.Parent:FindFirstChild('Hitbox') then
					local damageEvent = hit.Parent:FindFirstChild('damageEvent')
					if damageEvent then
						damageEvent:FireServer(script.Parent.Damage.Value)
						wait()
						canAttack = false
					end
				end
			end
		end)
		animTrack.KeyframeReached:Connect(function()
			canAttack = true
		end)
	end
	wait(.25)
	canAttack = true
end)

1 answer

You can add a variable, it states that whatever the tool is activated or not. When the tool is activated, you can turn the variable to true, when it's animation ended, (You know how long it is) the variable will turn back to false.

And then, you can make a completely different function.

When the hitbox is touched, it'll confirm that if it's a player or not and he's alive. And then it'll check if the activated variable is true or false. If it's true, it'll fire the remote event and make the server script do damage to the player. If it's false, nothing happens.

Hope that helped!

1 comment

0

This did not help at all, this is exactly what I already have. I want to make it so they can keep clicking to attack the monster (which has a custom health system using IntValues) and it will take damage away, or hold click where you will keep slashing with the tool and it will do damage.

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