I am attempting to make a script that will either change your avatar to something specific if your userid matches, or if your userid does not match it will choose between 3 different avatars. It changes your avatar by pasting one of the avatars into the StarterCharecter. It doesnt work as it will either put everyone in the special avatar, or put everyone in the randomly chosen avatars. Anyone have any idea whats going on? All responses are appreciated.
local Players = game:GetService("Players")
while wait(0.5) do
for i, v in pairs(Players:GetPlayers()) do
if v.UserId == 847431372 then
print("working")-- code here
game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
game.StarterPlayer.Characters.Special.StarterCharacter:Clone().Parent = game.StarterPlayer
print("Special Select")
else
local CharRound = math.random(1,3)
if CharRound == 1 then
game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
game.StarterPlayer.Characters.One.StarterCharacter:Clone().Parent = game.StarterPlayer
print("One Select")
elseif CharRound == 2 then
game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
game.StarterPlayer.Characters.Two.StarterCharacter:Clone().Parent = game.StarterPlayer
print("Two Select")
elseif CharRound == 3 then
game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
game.StarterPlayer.Characters.Three.StarterCharacter:Clone().Parent = game.StarterPlayer
print("Three Select")
end
end
end
end
I would also highly recommend that you make a table with all the starterCharacters like so:
local allStarterCharacter = {character1,character2,character3} -- You put all your playermodels in a table using {}
local pick = math.random(1,#allStarterCharacter ) -- By adding # behind a table, it gives you the amount of values it has (3 in this case)
local clone = allStarterCharacter[pick]:Clone() -- By doing this, you are asking for the random model defined with *pick* and you are cloning it. You can now do anything you want with it!
Okay i should of specified, though I dont know if you took this in account. This is supposed to be random on spawn, so when you load in the game its a new character. I cant really figure out the, disabling CharecterAutoLoads, system. And I cant really find anything good on it as its all for menus. Do you have anything else that might help?
Yeah I found that, I just dont know how to apply really any of this. Yes I can disable it, but I dont know what to do. Are there any articles or posts that can help me?
You can clone the model of the character you want from the script I sent you earlier, and then set the players character to it (player.Character = model)
Otherwise I would highly consider you checkout HumanoidDescription: HumanoidDescription | Roblox Creator Documentation
local Players = game:GetService("Players")
-- Set CharacterAutoLoads to false
Players.CharacterAutoLoads = false
-- Remove player's character from workspace on death
Players.PlayerAdded:Connect(function(player)
while true do
local char = player.CharacterAdded:Wait()
char.Humanoid.Died:Connect(function()
char:Destroy()
end)
end
end)
-- Respawn all dead players once every 10 seconds
while true do
local players = Players:GetChildren()
-- Check if each player is dead by checking if they have no character, if dead load that player's character
for _, player in pairs(players) do
if not workspace:FindFirstChild(player.Name) then
player:LoadCharacter()
end
end
-- Wait 10 seconds until next respawn check
task.wait(10)
end
But instead of calling load player clone your custom player (or random character) and set Player.Character to the new character.
Edit:
Actually don’t use that code. I swear the docs had actually useful code before the update. You should:
Create a respawn function that uses setting the Player.Character
Call the function when the player joins
Inside the respawn function create a died connection for the new character and respawn the character after X amount of time
Okay, im just getting confused. I wish I had more time scripting because hopefully I would know what im doing. Using alot of all of your advices I hobbled some code together, but honestly I dont really know how I would get it to work. Alot of errors and stuff not really making sense to me. Here it is.
local allStarterCharacter = {workspace.StarterCharacter1,workspace.StarterCharacter2,workspace.StarterCharacter3.StarterCharacterSP} -- You put all your playermodels in a table using {}
local pick = math.random(1,#allStarterCharacter ) -- By adding # behind a table, it gives you the amount of values it has (3 in this case)
local clone = allStarterCharacter[pick]:Clone()
local Players = game:GetService("Players")
Players.PlayerAdded:Wait()
for i, v in pairs(Players:GetPlayers()) do
if v.UserId == 847431372 then
pick then clone
else
end
end
local RESPAWN_TIME = 5
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
Players.CharacterAutoLoads = false
local function pickCharacter(player)
-- TODO: This function should return the character model for the player
end
local function loadCharacter(player)
local oldCharacter = player.Character
if oldCharacter then
oldCharacter:Destroy()
end
local newCharacter = pickCharacter(player):Clone()
-- There should be a humanoid in each character
local humanoid = newCharacter:WaitForChild("Humanoid")
-- TODO: position the character here (or maybe after the character is added to workspace, I'm not sure with the physics constraints. Maybe try this then the other.)
local connections = {}
-- Respawn after the humanoid dies
local diedConnection = humanoid.Died:Connect(function()
for _, connection in ipairs(connections) do
connection:Disconnect()
end
connections = nil
task.wait(RESPAWN_TIME)
loadCharacter(player)
end
-- Respawn if the humanoid is destroyed
local ancestryChangedConnection = humanoid.AncestryChanged:Connect(function()
for _, connection in ipairs(connections) do
connection:Disconnect()
end
connections = nil
if not humanoid:IsDescendantOf(Workspace) then
task.wait(RESPAWN_TIME)
loadCharacter(player)
end
end
table.insert(connections, diedConnection)
table.insert(connections, ancestryChangedConnection)
player.Character = newCharacter
newCharacter.Parent = Workspace
end
local function onPlayerAdded(player)
loadCharacter(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Okay I tested it out and noticed a few things. First there are some errors, and also I dont know where the code would get the charecter models. Thank you so much for all the time you spent, but I think im going to revert back and figure something else out, as before I implemented the character exception thing it worked fine.