How can I make accessories work in my team-based outfit assigner system?

Hello everyone! I am trying to help my younger brother with making his laser tag game. So I have taken most of the scripting process on my shoulders. I am trying my best.

Anyway, I made a system that assigns a specific outfit to the player depending on the team they are at (Blue or Red). I used two scripts for this.

The first one is a module script that stores the information for each outfit:

local TeamOutfits = {}

local player = game.Players.LocalPlayer


-- Function to set clothing
local function setOutfit(character, shirtTemplate, pantsTemplate)
    local shirt = character:FindFirstChildOfClass("Shirt")
    if not shirt then
        shirt = Instance.new("Shirt")
        shirt.Parent = character
    end
    shirt.ShirtTemplate = shirtTemplate

    local pants = character:FindFirstChildOfClass("Pants")
    if not pants then
        pants = Instance.new("Pants")
        pants.Parent = character
    end
    pants.PantsTemplate = pantsTemplate
end

-- Blue Team
function TeamOutfits.BlueTeam(character)

	setOutfit(character, "rbxassetid://68057457", "rbxassetid://68057790")

end

-- Red Team
function TeamOutfits.RedTeam(character)

	setOutfit(character, "rbxassetid://68057446", "rbxassetid://68057802")
	
end

-- For more outfits just copy and modify the above code 👍

return TeamOutfits

The second one is a local script in StarterPlayerScripts that does the assigning process (the print lines are simply for testing):

local player = game.Players.LocalPlayer
local Teams = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamOutfits = require(ReplicatedStorage.TeamOutfits)


-- The code that assigns the outfits depending on the team the player is on.
local function assignTeamOutfit(character)
    if player.Team == Teams.Red then
        print("Player is on red team")
        TeamOutfits.RedTeam(character)
    elseif player.Team == Teams.Blue then
        print("Player is on the blue team")
        TeamOutfits.BlueTeam(character)
    end
end

-- Give the player their respective outfit (if they exist)
if player.Character then
    assignTeamOutfit(player.Character)
end

-- Make outfit work even on respawn
player.CharacterAdded:Connect(function(character)
    assignTeamOutfit(character)
end)


It works very well so far, so I wanna expand on it further. I want to make it so that, alongside the clothes, it adds two accessories (four, but it’s two for each team) to the player. They’re not from the marketplace, they’re actually parts with meshes:


It seems they’re more complex to set up so I could really use some help here.

Thank you!

You can weld them using either Weld or weld constraint but its better to convert them into Accessory instance
https://create.roblox.com/docs/art/accessories

Code improvement tips:
Don’t index anything in runtime unless necessary, always cache
I recomend you removing module script entirely unless you use it across multiple scripts so compiler will inline function rather than calling it every time.
Always use getservice to ensure everything being loaded in
Also you seem to build visual purely on client, in simple games where character is static its a very good idea but you only assign it to a singular player aka the one that runs script
If you implement ragdolls just incase you should use original character for it rather than copy or else your system will not work.

Why does code looks ai generated lowkey tho?LOL

Also it fits Help and Feedback > Scripting Support category more i think

local Players = game:GetService("Players")
--local player = Players.LocalPlayer
local Teams = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamOutfits = require(ReplicatedStorage.TeamOutfits)
local BlueTeam,RedTeam = TeamOutfits.BlueTeam,TeamOutfits.RedTeam
local Red,Blue = Teams.Red,Teams.Blue

-- The code that assigns the outfits depending on the team the player is on.
local function assignTeamOutfit(character)
    if player.Team == Red then
        print("Player is on red team")
        RedTeam(character)
    elseif player.Team == Blue then
        print("Player is on the blue team")
        BlueTeam(character)
    end
end



local function plradded(plr:Player):()
plr.CharacterAdded:Connect(assignTeamOutfit)
local char = plr.Character::Model?
if char then assignTeamOutfit(char) end
end

-- Make outfit work even on respawn
Players.PlayerAdded:Connect(plradded)
for i,v in Players:GetPlayers() do plradded(v) end
1 Like

I will try to follow your help. As for the code, I decided to try out the scripting Assistant for the module script cause I was curious to see how it works. It was kinda 50/50 so I will try to learn coding by myself. Seems better in the long run. I’ll give an update soon. Thank you! :star:
Edit: I also took the code from the module script and added it to one script. Works the same!

Hello! I am letting you know that I made it all work great! I referenced this tutorial to convert my parts to accessories. I also realized I had mistakenly made the TeamOutfitAssigner script a local script. I adapted the code into a script in ServerScriptService and I fixed the issue in 5 minutes. Thank you for your help again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.