What I want to accomplish is a system that replaces a players character with a custom character. I want everyones character to be different based on what the first letter of their username is.
For instance, starting from A-F would be a green tuxedo and a Green Banded Top Hat. G-M would be a Blue Tuxedo and Blue Banded Top hat, and so forth. How would I do this?
You can use this code as a base for the other letters
local InsertService = game:GetService("InsertService")
local column1 = {12713819184, {"a", "b", "c", "d", "e", "f"}}
-- 12713819184 is the accessory id
task.wait(4)
for index, plr in pairs(game.Players:GetPlayers()) do
local letter = string.lower(string.sub(plr.Name, 1, 1))
local character = plr.Character
if table.find(column1[2], letter) then
local accessory = InsertService:LoadAsset(column1[1]):GetChildren()[1]
character.Humanoid:AddAccessory(accessory)
end
end
Gotcha, Would I just add onto the columns with the clothing ids? or would I have to do something else for that?
This code only supports one accessory, you would have to create more variables like column2 and column3 and change the letters.
Copying this obviously
if table.find(column1[2], letter) then -- change column1 with 2 and so on
local accessory = InsertService:LoadAsset(column1[1]):GetChildren()[1]
character.Humanoid:AddAccessory(accessory)
end
place the default characters in repstorage and name them Char1, Char2…
insert a localscript in starterplayerscripts:
local Char1 = {"A", "B", "C", "D", "E", "F"}
local Char2 = {"G", "H", "I", "J", "K", "L", "M"}
local Char3 = --finish this off and possibly Char4
function loadCharacter(char)
local clonedChar = repstorage[char]:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
game.Players.PlayerAdded:Connect(function(player)
local repstorage = game:GetService("ReplicatedStorage")
local firstChar = player.Name:sub(1, 1)
if table.find(Char1, firstChar) then
loadCharacter("Char1")
elseif table.find(Char2, firstChar) then
loadCharacter("Char2")
elseif table.find(Char3, firstChar) then
loadCharacter("Char3")
end
player.CharacterAdded:Once(function(character)
player:LoadCharacter()
end)
end)
yes, if you have Char4, modify the code to allow it
On line 7, there’s an error for ReplicatedStorage even though it’s made a variable in Line 13, why is that?
oh sorry my mistake
local repstorage = game:GetService("ReplicatedStorage")
local Char1 = {"A", "B", "C", "D", "E", "F"}
local Char2 = {"G", "H", "I", "J", "K", "L", "M"}
local Char3 = --finish this off and possibly Char4
function loadCharacter(char)
local clonedChar = repstorage[char]:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
game.Players.PlayerAdded:Connect(function(player)
local firstChar = player.Name:sub(1, 1)
if table.find(Char1, firstChar) then
loadCharacter("Char1")
elseif table.find(Char2, firstChar) then
loadCharacter("Char2")
elseif table.find(Char3, firstChar) then
loadCharacter("Char3")
end
player.CharacterAdded:Once(function(character)
player:LoadCharacter()
end)
end)
if you don’t want to make the modifications again, just move the variable to the top of the script
It doesn’t function as intended. The player doesn’t change into the desired character. It doesn’t give any errors and I’m not sure whats wrong with the script because of it.
It is probably the startercharacter not loading fast enough.
use this instead
local repstorage = game:GetService("ReplicatedStorage")
local Char1 = {"A", "B", "C", "D", "E", "F"}
local Char2 = {"G", "H", "I", "J", "K", "L", "M"}
local Char3 = --finish this off and possibly Char4
function loadCharacter(char)
local clonedChar = repstorage[char]:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
function waitAndLoadChar(player)
repeat task.wait() until game.StarterPlayer:FindFirstChild("StarterCharacter") ~= nil
player:LoadCharacter()
end
game.Players.PlayerAdded:Connect(function(player)
if game.StarterPlayer:FindFirstChild("StarterCharacter") then game.StarterPlayer.StarterCharacter:Destroy() end
local firstChar = player.Name:sub(1, 1)
if table.find(Char1, firstChar) then
loadCharacter("Char1")
elseif table.find(Char2, firstChar) then
loadCharacter("Char2")
elseif table.find(Char3, firstChar) then
loadCharacter("Char3")
end
player.CharacterAdded:Once(function()
coroutine.wrap(waitAndLoadChar)(player)
end)
end)
show me the current script so that i can modify, seeing that the error came from line 6, you probably made a mistake when making modifications
Nevermind, I fixed it. But it’s still not functioning as intended.
Here is the full script with every letter:
local repstorage = game:GetService("ReplicatedStorage")
local Char1 = {"A", "B", "C", "D", "E", "F"}
local Char2 = {"G", "H", "I", "J", "K", "L", "M"}
local Char3 = {"N", "O", "P", "Q", "R", "S", "T"}
local Char4 = {"U", "V", "W", "X", "Y", "Z"}
function loadCharacter(char)
local clonedChar = repstorage[char]:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
function waitAndLoadChar(player)
repeat task.wait() until game.StarterPlayer:FindFirstChild("StarterCharacter") ~= nil
player:LoadCharacter()
end
game.Players.PlayerAdded:Connect(function(player)
if game.StarterPlayer:FindFirstChild("StarterCharacter") then game.StarterPlayer.StarterCharacter:Destroy() end
local firstChar = player.Name:sub(1, 1)
if table.find(Char1, firstChar) then
loadCharacter("Char1")
elseif table.find(Char2, firstChar) then
loadCharacter("Char2")
elseif table.find(Char3, firstChar) then
loadCharacter("Char3")
elseif table.find(Char4, firstChar) then
loadCharacter("Char4")
end
player.CharacterAdded:Once(function()
coroutine.wrap(waitAndLoadChar)(player)
end)
end)
local repstorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local Char1 = {"A", "B", "C", "D", "E", "F"}
local Char2 = {"G", "H", "I", "J", "K", "L", "M"}
local Char3 = {"N", "O", "P", "Q", "R", "S", "T"}
local Char4 = {"U", "V", "W", "X", "Y", "Z"}
function loadCharacter(char)
local clonedChar = repstorage["Char"..tostring(char)]:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
function waitAndLoadChar()
game.StarterPlayer:WaitForChild("StarterCharacter")
player:LoadCharacter()
end
if game.StarterPlayer:FindFirstChild("StarterCharacter") then game.StarterPlayer.StarterCharacter:Destroy() end
local firstChar = player.Name:sub(1, 1)
if table.find(Char1, firstChar) then
loadCharacter(1)
elseif table.find(Char2, firstChar) then
loadCharacter(2)
elseif table.find(Char3, firstChar) then
loadCharacter(3)
elseif table.find(Char4, firstChar) then
loadCharacter(4)
end
player.PlayerAdded:Once(function()
coroutine.wrap(waitAndLoadChar)()
end)
few adjustments and also removed things that aren’t really ‘good’ for client sided scripts
Here is the adjusted script with some stuff changed around.
local repstorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local Column1 = {"A", "B", "C", "D", "E", "F"}
local Column2 = {"G", "H", "I", "J", "K", "L", "M"}
local Column3 = {"N", "O", "P", "Q", "R", "S", "T"}
local Column4 = {"U", "V", "W", "X", "Y", "Z"}
function loadCharacter(Character)
local clonedChar = repstorage["Character"..tostring(Character)]:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
function waitAndLoadChar()
game.StarterPlayer:WaitForChild("StarterCharacter")
player:LoadCharacter()
end
if game.StarterPlayer:FindFirstChild("StarterCharacter") then game.StarterPlayer.StarterCharacter:Destroy() end
local firstChar = player.Name:sub(1, 1)
if table.find(Column1, firstChar) then
loadCharacter(1)
elseif table.find(Column2, firstChar) then
loadCharacter(2)
elseif table.find(Column3, firstChar) then
loadCharacter(3)
elseif table.find(Column4, firstChar) then
loadCharacter(4)
end
player.PlayerAdded:Once(function()
coroutine.wrap(waitAndLoadChar)()
end)
This does now produce a valid error, that being this:
oop, just replace player.PlayerAdded
with game.Players.PlayerAdded
So many elseifs for what?
function CheckForOutfit(str)
return ServerStorage.Outfits:FindFirstChild(str) -- I presume you're holding outfits here?
end
print(CheckForOutfit(("LPlayerName"):sub(1,1))) -- This will return L, you can utilize it.
it’s clientsided,
he has 4 outfits for multiple letters and not individual
If it’s clientsided then he can just do this?
function CheckForInstance(str,parent)
for _,v in parent:GetChildren() do -- Ideally please store outfits in a folder!
if tostring(v):sub(1,#str):match(str) then
return v
end
end
return nil
end
function loadCharacter(str)
if CheckForInstance(str, repstorage) == nil then return end
local clonedChar = CheckForInstance(str, repstorage):Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game.StarterPlayer
end
loadCharacter(Player.Name:sub(1,1))