So I have a script that forces players to have team uniforms (cannot opt out of), and adds a flak vest model when they spawn. However, I handle this on the server and when a round ends, all players get respawned and lag occurs because the server has to delete the player’s clothes, apply the forced uniforms, and clone the vest and weld it to the player.
This causes a lot of lag in my game and I was wondering if I could handle these on the client.
Here’s the code for the team uniform giver:
local Teams = game:GetService("Teams")
function remove(c) --removes obnoxious accessories
for i,v in pairs(c:GetChildren()) do
if v:IsA("Accessory") then
local hand = v:FindFirstChild("Handle")
if hand then
for e, r in pairs(hand:GetChildren()) do
if r:IsA("Attachment") then
if r.Name == "WaistBackAttachment" or r.Name == "BodyFrontAttachment" or r.Name == "BodyBackAttachment" or r.Name == "RightShoulderAttachment" or r.Name == "LeftShoulderAttachment" then
wait()
v:Destroy()
end
end
end
end
end
end
end
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(Char)
local female = false
Char.ChildAdded:Connect(function(child)
remove(Char)
if child:IsA("CharacterMesh") then --detects if player is wearing a "female" body mesh
if child.MeshId == 82987757 then
child.MeshId = 48112070
female = true
elseif child.MeshId == 48112070 then
female = true
else
wait()
child:Destroy()
end
end
end)
wait(0.5)
local Folder = script:FindFirstChild(p.Team.Name) --applies uniforms here
if Folder then
local Clothes = Folder:FindFirstChild("Clothes")
if Clothes then
for i,v in pairs(Clothes:GetChildren()) do
local Clothing = Char:FindFirstChild(v.Name)
if Clothing then
Clothing:Destroy()
end
v:Clone().Parent = Char
end
end
wait()
local Vest = Folder:FindFirstChild("Vest") --applies flak vests here
if Vest and female == false then
local g = Vest.Vest:clone()
g.Parent = Char
local C = g:GetChildren()
for i=1, #C do
if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
local W = Instance.new("Weld")
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new("Weld")
Y.Part0 = Char.Torso
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end
local h = g:GetChildren()
for i = 1, # h do
h[i].Anchored = false
h[i].CanCollide = false
end
end
end
end)
end)