Hello, I have been having a serious issue with my game lately. The problem is that I made a script for punching on every player’s character, yet something odd happens.
Intended result: Both players are able to punch eachother with their own different hitboxes
Actual result: When in a local server with two players, it seems the last one who joined gets priority for every player, meaning that the last player (call them player2) can punch for itself, yet when player1 (plr who joined before) tries to punch, it plays the animation and hitbox on player2
A notable issue is that when player1 dies the priority is transferred to them and the glitch happens to player2.
Video:
Code:
--ServerScriptService
local SS = game:GetService("ServerStorage")
local RepStorage = game:GetService("ReplicatedStorage")
local Plrs = game:GetService("Players")
local RunService = game:GetService("RunService")
local Events = RepStorage:WaitForChild("Events")
local PunchEvent = Events:WaitForChild("PunchEvent")
local HitboxClass = require(RepStorage:WaitForChild("Modules"):WaitForChild("HitboxClass"))
local HitboxTypes = require(RepStorage:WaitForChild("Modules"):WaitForChild("HitboxClass"):WaitForChild("Types"))
local Animations = SS:WaitForChild("Animations")
local PlrAnims = Animations:WaitForChild("Player")
local LoadedAnims = {}
local combo = 0
local hand = 0
local maxComboTime = 2
local comboTime = 0
local Range = 5
local debounce = false
Plrs.PlayerAdded:Connect(function(plr)
print(plr.Name,"joined")
plr.CharacterAppearanceLoaded:Connect(function(char)
local humanoid:Humanoid = char:WaitForChild("Humanoid")
local animator:Animator = humanoid:WaitForChild("Animator")
local HumanoidRootPart:Part = char:WaitForChild("HumanoidRootPart")
local function getAnim(plr:Player,name:string)
for i,v in pairs(LoadedAnims[plr.Name]) do
if v.Name == name then
return v
end
end
end
local function stopAllPlayingAnims(time)
for _, v:AnimationTrack in pairs(animator:GetPlayingAnimationTracks()) do
if time == nil then
v:Stop()
else
v:Stop(time)
end
end
end
local AnimFolder = Instance.new("Folder")
AnimFolder.Parent = char
AnimFolder.Name = "AnimFolder"
local tempTable = {}
for _, anim:Animation in pairs(PlrAnims:GetChildren()) do
local newAnim = Instance.new("Animation")
newAnim.Parent = AnimFolder
newAnim.AnimationId = anim.AnimationId
newAnim.Name = anim.Name
local loadedAnim = animator:LoadAnimation(newAnim)
table.insert(tempTable, loadedAnim)
end
LoadedAnims[plr.Name] = tempTable
print("Loaded animations:",LoadedAnims)
PunchLeft = getAnim(plr,"PunchLeft")
PunchRight = getAnim(plr,"PunchRight")
Uppercut = getAnim(plr,"Uppercut")
Dropkick = getAnim(plr,"Dropkick")
local hitboxParams:HitboxTypes.HitboxParams = {
SizeOrPart = Vector3.new(3,4,3.5),
InitialPosition = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.LookVector * Range/2,
DebounceTime = 0.5,
Debug = true,
UseClient = plr,
Blacklist = {char}
}
checkHitbox = HitboxClass.new(hitboxParams)
checkHitbox:WeldTo(HumanoidRootPart, CFrame.new(0,0,-2))
checkHitbox.HitSomeone:Connect(function(hitChars)
print("hit someone")
combo += 1
comboTime = 0
print(combo)
if combo == 3 then
Uppercut:Play(0.1, 5, 1.5)
elseif combo == 4 then
Dropkick:Play(0.1, 5, 1.5)
combo = 0
end
for _, hitChar in pairs(hitChars) do
local hitHumanoid = hitChar:FindFirstChildOfClass("Humanoid")
local hitPlr = Plrs:GetPlayerFromCharacter(hitChar)
hitHumanoid.Health -= 5
getAnim(hitPlr,"Hit"):Play()
end
end)
task.spawn(function()
while true do
wait(1)
if combo > 0 then
comboTime += 1
if comboTime >= maxComboTime then
combo = 0
end
end
end
end)
wait(5)
PunchLeft:Play()
end)
end)
PunchEvent.OnServerEvent:Connect(function(plr)
if debounce then return end
debounce = true
if combo < 3 then
if hand == 0 then
hand = 1
PunchLeft:Play(0.1, 5, 1.5)
elseif hand == 1 then
hand = 0
PunchRight:Play(0.1, 5, 1.5)
end
end
checkHitbox:Start()
task.wait(0.2)
checkHitbox:Stop()
task.wait(0.2)
debounce = false
end)
--StarterCharacterScripts
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local RepStorage = game:GetService("ReplicatedStorage")
local Plrs = game:GetService("Players")
local plr = Plrs.LocalPlayer
local char = plr.Character
local humanoid:Humanoid = char:WaitForChild("Humanoid")
local HumanoidRootPart:Part = char:WaitForChild("HumanoidRootPart")
local jumpUsage = 1
local PunchEvent = RepStorage:WaitForChild("Events"):WaitForChild("PunchEvent")
CAS:BindAction("Punch", function(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
PunchEvent:FireServer()
end
end, true, Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2)