I had a battlegrounds combat game using weapons. And when activating the tool, the slashing animation is delayed up to 1 seconds in servers with 4+ people. How do i fix this?
Here’s the serverscript (child of the weapon)
local Tool = script.Parent
Tool.Enabled = true
local Handle = Tool:WaitForChild("Handle")
local Sounds = {
Hit = Handle:WaitForChild("Hit"),
Swing = Handle:WaitForChild("Swing")
}
local Animations = {}
local comboAnim = 1
local hittable = false
local Services = {
Players = (game:FindService("Players") or game:GetService("Players")),
Debris = (game:FindService("Debris") or game:GetService("Debris")),
}
local events = {}
local Player,Character,Humanoid
function IsTeamMate(Player1, Player2)
return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end
function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Creator_Tag.Parent = humanoid
Services.Debris:AddItem(Creator_Tag, 7.5)
end
function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
function IsInTable(Table,Value)
for _,v in pairs(Table) do
if v == Value then
return true
end
end
return false
end
local HitHumanoids = {}
function Activated()
if not Tool.Enabled or not Humanoid or Humanoid.Health <= 0 then return end
Tool.Enabled = false
delay(script.Parent.Cooldown.Value,function()
HitHumanoids = {}
Tool.Enabled = true
end)
Sounds.Swing.PlaybackSpeed = math.random()*(1.3-0.7) + 0.7
Sounds.Swing:Play()
if comboAnim == 1 then
comboAnim = 2
Animations.Chop1:Play()
delay(.1, function()
script.Parent.Blade.Trail.Enabled = true
end)
hittable = true
Animations.Chop1.Stopped:Wait()
hittable = false
script.Parent.Blade.Trail.Enabled = false
else
comboAnim = 1
Animations.Chop2:Play()
delay(.1, function()
script.Parent.Blade.Trail.Enabled = true
end)
hittable = true
Animations.Chop2.Stopped:Wait()
hittable = false
script.Parent.Blade.Trail.Enabled = false
end
end
function Touched(hit)
if not hit or not hit.Parent or Tool.Enabled or not hittable then return end
local hum,ff = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
if not hum or hum.Health <= 0 or ff or IsTeamMate(Services.Players:GetPlayerFromCharacter(hum.Parent),Player) or IsInTable(HitHumanoids,hum) then return end
HitHumanoids[#HitHumanoids+1]=hum
UntagHumanoid(hum)
TagHumanoid(hum,Character)
hum:TakeDamage(Tool.Damage.Value)
Sounds.Hit.PlaybackSpeed = math.random()*(1.3-0.7) + 0.7
Sounds.Hit:Play()
local delta = Character.HumanoidRootPart.CFrame.LookVector
local Velocity = Instance.new("BodyVelocity", hum.Parent.Head)
Velocity.P = 1000
Velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
Velocity.Velocity = delta.unit * script.Parent.Knockback.Value
Velocity.Velocity = Velocity.Velocity + Vector3.new(0,10,0)
local gp = game:GetService("MarketplaceService")
if gp:UserOwnsGamePassAsync(Player.UserId, 135186641) and gp:UserOwnsGamePassAsync(Player.UserId, 135187147) then
Player.leaderstats.Slashes.Value += 5
elseif gp:UserOwnsGamePassAsync(Player.UserId, 135186641) then
Player.leaderstats.Slashes.Value += 2
elseif gp:UserOwnsGamePassAsync(Player.UserId, 135187147) then
Player.leaderstats.Slashes.Value += 3
else
Player.leaderstats.Slashes.Value += 1
end
local rd = script.Parent.Ragdoll:Clone()
rd.Parent = hit.Parent
rd.Enabled = true
script.Parent.Cooldown.Value -= 0.2
delay(script.Parent.Cooldown.Value + 0.1, function()
script.Parent.Cooldown.Value += 0.2
end)
game.Debris:AddItem(Velocity, 0.3)
end
function Equipped()
Character = Tool.Parent
Player = Services.Players:GetPlayerFromCharacter(Character)
Humanoid = Character:FindFirstChildOfClass("Humanoid")
if not Humanoid or Humanoid.Health <= 0 then return end
Animations = {
Chop1 = Humanoid:FindFirstChild("Animator"):LoadAnimation(Tool:WaitForChild("Chop1")),
Chop2 = Humanoid:FindFirstChild("Animator"):LoadAnimation(Tool:WaitForChild("Chop2"))
}
events[#events+1] = Tool.Activated:Connect(Activated)
events[#events+1] = script.Parent.Blade.Touched:Connect(Touched)
Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
end
function Unequipped()
for i=1,#events do
if events[i] then events[i]:Disconnect();events[i] = nil end
end
end
Tool.Equipped:Connect(Equipped)