Hello dear devs! Today ill be showing y’all how to make a simple fist system.
First,create a remoteEvent in replicated storage and then create a local script in StarterPlayer ----> StarterCharacterScripts and insert this in!
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local anim2 = Instance.new("Animation")
local playerArms = player.Character:WaitForChild("RightHand") -- The fist
local remoteEvent = game:GetService("ReplicatedStorage").FistEvent -- change "FistEvent" to the name of your remote event!
mouse.Button1Down:Connect(function()
if playerArms then
remoteEvent:FireServer(player)
anim2.Name = "PunchAnim"
anim2.AnimationId = "rbxassetid://your_animation_id"
local track
track = script.Parent.Humanoid.Animator:LoadAnimation(anim2)
track.Priority = Enum.AnimationPriority.Action4
track:Play()
script.Enabled = false
wait(2)
script.Enabled = true
end
end)
after you’ve done that insert a script inside “serverscriptservice” and put this in!
game.ReplicatedStorage.FistEvent.OnServerEvent:Connect(function(player)
local playerArms = player.Character:FindFirstChild("RightHand")
playerArms.Touched:Connect(function(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid then
local plr = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if humanoid.Health > 0 then
humanoid:TakeDamage(20)
script.Enabled = false
print("Damaged")
wait(2)
script.Enabled = true
end
end
end)
end)
I know it’s kinda off topic but i just wanted to ask if the hitbox should be handled client or server side both of them have adventanges and disadventages, just wanted to know wich one is better
hitboxes client sided are more accurate, you should do them with sanity checks on the server. for example making sure the player youre hitting is within reasonable distance.
This looks really messy and really impractical. You should format and space out your code to make it more readable to other developers, especially since this is targeted towards newer ones. You should also explain what your code does a bit more.
Instead of .touched you can use GetPartBoundsInBox/GetPartBoundsInRadius. You might need to study the parameters a bit, but when you learn it, it’s easy to work with. As people above me said, you should also do hitboxes on the client side, but with checks on the server, and use task.wait instead of wait.
i used action4 in case a player has other animations, and the other animations may ruin the fist animation, to be honest action may also do the same thing as action4 but i just use action4 everytime.
Do you have any visuals for what this is supposed to end up looking like? Videos and/or a place with the code would be a nice thing to have in the post.