Hi there i have a problem with my punching system and I’m not sure how to fix it.
My punching damage only plays when its very very close to a player here’s an example:
Here are the codes i have used:
StarterCharacterScripts:
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)
local punchsound = game.Workspace.Punchsound -- sound in workspace
PunchAnimation:Play()
punchsound:play()
Punch:FireServer()
wait(1)
CanDamage = true
end
end
end)
ServerScriptService:
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(10)
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)
I would like to make that the damage starts at 1-2 studs away from player.
Any help counts thanks.