I made a simple punch script, where the player needs to press ‘F’ to punch. Every punch can only deal damage to one person (20 for each punch).
PunchTest.rbxl (28.4 KB)
Workspace:
RemoteHandler:
local RStorage = game:GetService("ReplicatedStorage")
local Remotes = RStorage:WaitForChild("Remotes")
local Punch = Remotes:WaitForChild("Punch")
local Connection
local function DealDamage(Part)
local Character = Part.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(20)
Connection:Disconnect()
end
end
local function PlayerPunch(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local RightHand = Character:WaitForChild("RightHand")
Connection = RightHand.Touched:Connect(DealDamage)
wait(0.5)
Connection:Disconnect()
end
Punch.OnServerEvent:Connect(PlayerPunch)
Local Script:
local Players = game:GetService("Players")
local RStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Remotes = RStorage:WaitForChild("Remotes")
local Punch = Remotes:WaitForChild("Punch")
local Mouse = Player:GetMouse()
local Animation = script:WaitForChild("Animation")
local CanDamage = true
Mouse.KeyDown:connect(function(Key)
if Key == "f" then
if CanDamage then
CanDamage = false
local Humanoid = Character:WaitForChild("Humanoid")
local PunchAnimation = Humanoid:LoadAnimation(Animation)
PunchAnimation:Play()
Punch:FireServer()
wait(1)
CanDamage = true
end
end
end)
I got a few points where I’m not sure whether I can do like I did.
- Shall I assign Connection and DealDamage() (both in RemoteHandler) inside the PlayerPunch function or leave it as it is?
- Is it dangerous to make a debounce on the client to not fire a RemoteEvent whenever ‘F’ is pressed? If so, are there other possibilities to not fire a RemoteEvent every time?
- I had a ‘bug’ where one punch did way more damage than supposed (Can’t replicate it). Do any of my scripts have vulnerabilities?