This script works but is there a better/easier way of scripting this? - This script just clones a part to each character in the game and welds it to their own torso.
while wait() do local Part = game.ReplicatedStorage:WaitForChild('Cop') local plrs = game.Players:GetPlayers() for i = 1, #plrs do local Char = plrs[i].Character local Torso = Char.Torso if not Torso:FindFirstChild('Cop') then local clonedPart = Part:Clone() clonedPart.Parent = Torso local Weld = Instance.new('Weld', Torso) Weld.Part0 = Torso Weld.Part1 = clonedPart Weld.C0 = CFrame.new(0,0,0) end end end
Try using the PlayerAdded
and CharacterAdded
events:
local part = game:ReplicatedStorage:WaitForChild("Cop") game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(char) local torso = char:WaitForChild("Torso") local n = part:Clone() n.Parent = char local weld = Instance.new("Weld",n) weld.Part0 = n weld.Part1 = torso end) end)
Hope I helped!
~TDP
If you think this script is confusing, you can split this script into functions. Eg. you can type a new function for welding process by typing:
function Welding(p1,p2) local w = Instance.new('Weld', p1) w.Part0 = p1 w.Part1 = p2 w.C0 = CFrame.new(0,0,0)
and you can call this function from while loop by typing:
Welding(Torso,clonedPart)
Edit: Suggestion of TheDeadlyPanther is pretty good too, you can use PlayerAdded and CharacterAdded events to make it less laggy and better process. Also if you use LocalScript you can reach character each time a character spawns. Give it a try, LocalScripts are one of the most lagless and easy type of scripts if you are working on player.