Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Team-Based Characters not working. Help?

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)

1 answer

A player's TeamColor is not a string. Please use BrickColor.new(Color) instead.

Here's a link to the page about Player.TeamColor: https://developer.roblox.com/en-us/api-reference/property/Player/TeamColor

Also, based on the script.Parent.Parent, is this in a LocalScript?

If so, please make sure this is in a regular script inside of either Workspace or ServerScriptService.

local StarterCharacter1 = game.ServerStorage.TModel
local StarterCharacter2 = game.ServerStorage.CTModel

game.Players.PlayerAdded:connect(function(plr)
    if plr.TeamColor == BrickColor.new("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
    elseif plr.TeamColor == BrickColor.new("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

0

The script is a global script placed inside StarterPack.

0

I was trying to access the player by putting the script inside StarterPack and using script.Parent.Parent

0

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.

0

I have tried doing that but I don't know how to check the TeamColor of the player through ServerScriptService.

0

Try the code I have edited into my answer.

0

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.