So, I'm trying to make a game like CS:GO on Roblox, but I have a problem. I am having problems making team-based characters.
Let's say everyone in Team 1 is supposed to have Character 1 and Team 2 is supposed to have Character 2. My attempt was to clone Character 1 into StarterPlayer and rename it to StarterCharacter if the player is inside Team 1. Then, when the player's character has loaded, StarterCharacter gets removed. The same with Team 2.
Now, when I loaded in, my character appearance didn't change and I was still me. The script is a serverscript and is placed inside StarterPack. I am also getting no error in the output.
Here's the code: (I'm kinda new to scripting)
local StarterCharacter1 = game.ServerStorage.TModel
local StarterCharacter2 = game.ServerStorage.CTModel
--Terrorist Side
game.Players.PlayerAdded:connect(function(plr)
if script.Parent.Parent.TeamColor == ("Maroon") then
StarterCharacter1:Clone().Parent = game.StarterPlayer
game.StarterPlayer.TModel.Name = "StarterCharacter"
local Pos = plr.Character:GetPrimaryPartCFrame()
plr:LoadCharacter()
plr.Character:SetPrimaryPartCFrame(Pos)
if plr.Character:FindFirstChild("ForceField") then
plr.Character["ForceField"]:Destroy()
game.StarterPlayer.StarterCharacter:Remove()
end
end
end)
--Counter Terrorist Side
game.Players.PlayerAdded:connect(function(plr)
if script.Parent.Parent.TeamColor == ("Navy Blue") then
StarterCharacter2:Clone().Parent = game.StarterPlayer
game.StarterPlayer.CTModel.Name = "StarterCharacter"
local Pos = plr.Character:GetPrimaryPartCFrame()
plr:LoadCharacter()
plr.Character:SetPrimaryPartCFrame(Pos)
if plr.Character:FindFirstChild("ForceField") then
plr.Character["ForceField"]:Destroy()
game.StarterPlayer.StarterCharacter:Remove()
end
end
end)

6 comments
The script is a global script placed inside StarterPack.
I was trying to access the player by putting the script inside StarterPack and using script.Parent.Parent
You might want to try making this into a script inside of ServerScriptService, because if you have, say, 10 players, running the same connections events, it will cause lag.
I have tried doing that but I don't know how to check the TeamColor of the player through ServerScriptService.
Try the code I have edited into my answer.
The character does load in as the custom character, but I am getting this error: ServerScriptService.Teams:8: attempt to index field 'Character' (a nil value). Also, the StarterCharacter stays inside StarterPlayer.