How to make a certain roblox player will spawn as custom character when joined?

Hello Devs!
I’ve recently made a custom character script that works fine until a second player joins.

  1. What do you want to achieve?
    I would like to make it so that when a certain Roblox player joins the game, they will spawn as a custom character differently from every other player.

  2. What is the issue?
    The script works fine, Only for one player though. When a second player joins then they have the same custom character as the certain player.

  3. What solutions have you tried so far?
    I’ve searched through a bunch of videos, but cant seem to repurpose their scripts or I just don’t understand their script.

Here is my current script:

local Name = "WH_HAN" 
game.Players.PlayerAdded:connect(function(plr) 
	plr.CharacterAdded:connect(function(char)
		wait(1)
		if char.Name:lower() == Name:lower() then
			game.ServerStorage.WH.StarterCharacter.Parent = game.StarterPlayer
			char.Humanoid.Health = 0
			wait(5.1)
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(script.Parent.ToPart.Position) --This part teleports the player to the main spawn area.
		else
			game.ServerStorage.NormalPlayers.StarterCharacter.Parent = game.StarterPlayer
			char.Humanoid.Health = 0
			wait(5.1)
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(script.Parent.ToPart.Position) --This part teleports the player to the main spawn area.
		end
	end)
end)

If anyone could please help me out, or point me in the right direction that would be greatly appreciated!

(If it isn’t clear on what I would like to achieve, please let me know)

2 Likes

What is the lower function you have?

what do you mean by “Lower function”?

char.Name:lower() == Name:lower()

To be fair, I got help for part of this code. Also, I don’t quite understand what your question is…

I believe you’re only setting the StarterCharacter’s parent? Do you have any other characters that would be different than the player that first joined?

I see, the script is setting everyone’s character instead of just one player. Also, I do have multiple different characters that would be different from my character.

I see, well you could get all the children of the characters and select one using math.random perhaps? That can work

Inside your else statement:

local AvailableCharacters = game.ServerStorage.NormalPlayers:GetChildren()
local RandomCharacter = AvailableCharacters[math.random(1, AvailableCharacters)]
RandomCharacter.Parent = game.StarterPlayer
1 Like

I just tested some other code, and it works but now (sorta) whenever the player joins the game the custom character glitches out it spawns and deletes multiple of the same startercharacter inside my player, which is confusing because I don’t think my script is a closed-loop?

local value = 0

game.Players.PlayerAdded:connect(function(plr)
    value = 0
end)

game.Players.PlayerAdded:connect(function(plr) 
    plr.CharacterAdded:connect(function(char) 
        wait(1)
        if plr.Name == "WH_HAN" and value == 0 then 
            local model = game.ReplicatedStorage.King.StarterCharacter
            local model1 = model:Clone()
            model1.Parent = game.Workspace
            model1.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
            plr.Character = model1
            model1.Humanoid.HipHeight = 3.8
            value = 1
        end
    end)
end)

Really now? Can I see your current script if possible?

Sorry I forgot how to put the lua thing for a sec, I just edited it.

Also, If your wondering what the glitching looks like I just took this image:image
(also yes my custom character is a toaster with wheels)

1 Like

I got it working :DD (idk how but I did)
Heres the code:

local value = 0

game.Players.PlayerAdded:connect(function(plr)
	value = 0
end)


game.Players.PlayerAdded:connect(function(plr) 
	plr.CharacterAdded:connect(function(char) 
		wait(1) --wait a second
		if plr.Name == "WH_HAN" and value == 0 then
			local model = game.ReplicatedStorage.King.StarterCharacter
			local model1 = model:Clone()
			model1.Parent = game.Workspace
			model1.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model1
			model1.Humanoid.HipHeight = 3.8
			value = 1
		else if plr.Name == "Kaidawa" and value == 0 then 
				local model = game.ReplicatedStorage.Dev.StarterCharacter
				local model2 = model:Clone()
				model2.Parent = game.Workspace
				model2.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
				plr.Character = model2
				model2.Humanoid.HipHeight = 3.8
				value = 1
			else if plr.Name == "fart1023" and value == 0 then 
					local model = game.ReplicatedStorage.Dev.StarterCharacter
					local model3 = model:Clone()
					model3.Parent = game.Workspace
					model3.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
					plr.Character = model3
					model3.Humanoid.HipHeight = 3.8
					value = 1
				else if plr.Name == "enderdragon1784" and value == 0 then 
						local model = game.ReplicatedStorage.Dev.StarterCharacter
						local model4 = model:Clone()
						model4.Parent = game.Workspace
						model4.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
						plr.Character = model4
						model4.Humanoid.HipHeight = 3.8
						value = 1
					else if plr.Name == "Jackscarlett" and value == 0 then 
							local model = game.ReplicatedStorage.Dev.StarterCharacter
							local model4 = model:Clone()
							model4.Parent = game.Workspace
							model4.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
							plr.Character = model4
							model4.Humanoid.HipHeight = 3.8
							value = 1
						else if value == 0 then 
								local model8 = game.ReplicatedStorage.NormalPlayers.StarterCharacter
								local model14 = model8:Clone()
								model14.Parent = game.Workspace
								model14.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
								plr.Character = model14
								model14.Humanoid.HipHeight = 3.8
								value = 1
							end	
						end
					end
				end
			end
		end
	end)
end)

This code is probably very inefficient, but it works!

Also thanks Jack for helping me out! (I’ll give u a Dev skin in the game for helping me)
Link to the game:

https://www.roblox.com/games/6720660296/Toaster-Island-2-WIP

2 Likes

Oh mah gosh please change your else if’s to elseif’s

There’s literally no reason to be spacing them out and adding more conditional checks than usual

game.Players.PlayerAdded:connect(function(plr) 
    local value = 0
	plr.CharacterAdded:connect(function(char) 
		wait(1) --wait a second
		if plr.Name == "WH_HAN" and value == 0 then
			local model = game.ReplicatedStorage.King.StarterCharacter
			local model1 = model:Clone()
			model1.Parent = game.Workspace
			model1.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model1
			model1.Humanoid.HipHeight = 3.8
			value = 1
		elseif plr.Name == "Kaidawa" and value == 0 then 
			local model = game.ReplicatedStorage.Dev.StarterCharacter
			local model2 = model:Clone()
			model2.Parent = game.Workspace
			model2.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model2
			model2.Humanoid.HipHeight = 3.8
			value = 1
		elseif plr.Name == "fart1023" and value == 0 then 
			local model = game.ReplicatedStorage.Dev.StarterCharacter
			local model3 = model:Clone()
			model3.Parent = game.Workspace
			model3.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model3
			model3.Humanoid.HipHeight = 3.8
			value = 1
		elseif plr.Name == "enderdragon1784" and value == 0 then 
			local model = game.ReplicatedStorage.Dev.StarterCharacter
			local model4 = model:Clone()
			model4.Parent = game.Workspace
			model4.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model4
            model4.Humanoid.HipHeight = 3.8
			value = 1
		elseif plr.Name == "Jackscarlett" and value == 0 then 
			local model = game.ReplicatedStorage.Dev.StarterCharacter
			local model4 = model:Clone()
			model4.Parent = game.Workspace
			model4.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model4
			model4.Humanoid.HipHeight = 3.8
			value = 1
		elseif value == 0 then 
			local model8 = game.ReplicatedStorage.NormalPlayers.StarterCharacter
			local model14 = model8:Clone()
			model14.Parent = game.Workspace
			model14.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model14
			model14.Humanoid.HipHeight = 3.8
			value = 1
		end	
	end)
end)
3 Likes

Sorry for that, Good to know! I have improved the code and shortened it to 39 lines instead.

local value = 0
local mods = {"", "", "", ""}

game.Players.PlayerAdded:connect(function(plr)
	value = 0
end)

game.Players.PlayerAdded:connect(function(plr) 
	plr.CharacterAdded:connect(function(char) 
		wait(1) 
		if plr.Name == "" and value == 0 then
			local model = game.ReplicatedStorage.King.StarterCharacter
			local model1 = model:Clone()
			model1.Parent = game.Workspace
			model1.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model1
			model1.Humanoid.HipHeight = 3.8
			value = 1
		elseif table.find(mods,plr.Name) and value == 0 then
			local model = game.ReplicatedStorage.Dev.StarterCharacter
			local model2 = model:Clone()
			model2.Parent = game.Workspace
			model2.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model2
			model2.Humanoid.HipHeight = 3.8
			value = 1
		elseif value == 0 then 
			local model3 = game.ReplicatedStorage.NormalPlayers.StarterCharacter
			local model4 = model3:Clone()
			model3.Parent = game.Workspace
			model3.PrimaryPart.CFrame = plr.Character.PrimaryPart.CFrame 
			plr.Character = model3
			model3.Humanoid.HipHeight = 3.8
			value = 1
		end
	end)
end)
2 Likes