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

How would i make the players character change on textbutton click?

How would I make the players character change on textbutton click? I mean I have an idea but it doesnt work. I thought it would be somethin like

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local char = game:WaitForChild('ServerStorage').CustomCharacter
	player.Character = char

how would I do it?

3 answers

Don't try to access Server Storage via LocalScript as it will not work. Bcz Server Storage is accessable by server only.

This is not going to be a simple thing..

First, Local script in TextButton.

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.YOUR_REMOTE_EVENT_NAME:FireServer()
end

Then, the Server script in ServerScriptService.

local function applyBodyColors(player, item, RigType)
	local char = player.Character
	item.Parent = char --Here, item is Body Color of the character
end

local function applyBodyPart(player, item, RigType)
	if RigType == "R15" then
		local replace = player.Character:FindFirstChild(item.Name)
	
		if replace then
			item.Parent = player.Character
			replace:Destroy()
		end

		player.Character.Humanoid:BuildRigFromAttachments()

	else -- If, the Rig type isn't R15

		for _, oldPart in ipairs(player.Character:GetChildren()) do
			if oldPart.ClassName == "CharacterMesh" then
				if oldPart.Name == item.BodyPart.Name then
					oldPart:Destroy()
				end
			end
		end
		item.Name = item.BodyPart.Name 
		item.Parent = player.Character
	end
end

local function AddItemToCharacter(player, item, rigType)
	local Type = item.ClassName

	if Type == "Accessory" or Type == "Shirt" or Type == "Pants" or Type == "Hats" then
		item.Parent = player.Character
	elseif Type == "Tool" then
		item.Parent = player.Backpack

	elseif item.Name == "Head" then
		player.Character.Head.Mesh:Destroy()
		item.Mesh.Parent = player.Character.Head
		player.Character.Head.face:Destroy()
		item.face.Parent = player.Character.Head
		player.Character.Head.Color = item.Color

	elseif Type == "MeshPart" then
		applyBodyPart(player, item, rigType)

	elseif Type == "BodyColors" then
		applyBodyColors(player, item, rigType)
	end
end

local function loadCharacter(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local RigType = Character.Humanoid.RigType == Enum.HumanoidRigType.R15 and "R15" or "R6"
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local NewCharacter = ReplicatedStorage:WaitForChild("Character")

	if NewCharacter then
		if player:HasAppearanceLoaded() == false then
			repeat until player:HasAppearanceLoaded() == true
		end
	
		player:ClearCharacterAppearance()

		local EquipCharacter = NewCharacter:Clone()
		for _, item in pairs(EquipCharacter:GetChildren()) do
			AddItemToCharacter(player, item, RigType)
		end
	end
end

game.ReplicatedStorage.YOUR_REMOTE_EVENT_NAME.OnServerEvent:Connect(loadCharacter)

It's too long I know and just don't copy/paste. Try to understand the script first.

Make sure to add Remote Event in ReplicatedStorage and rename it to that you will be using in the script.

Also, add the character u want to give the player in ReplicatedStorage.

Lemme know if it helps! Please do give an Upvote if it helped you bcz the script took really long time to write.

2 comments

0

imma give you an update wether it worked or not cause gosh darn thats a long script

0

UPVOTE*

You need to (locally) clone CustomCharacter to game.StarterPlayer And then name it “StarterCharacter” and do Player:LoadCharacter()

4 comments

0

but won't that make STARTER character?

1

StarterCharacter is the char the player spawns with. If you want to revert it when they die you can use Humanoid.Died to do StarterCharacter:Destroy()

0

Youre also using Player:LoadCharacter() to load the new starter character.

1

Did this work?

COMBO of 2 Replies.

script 1: (localscript inside button)

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.changecharacter:FireServer()
end)

script2(script inside serverscriptservice)

game.ReplicatedStorage.changecharacter.OnServerEvent:Connect(function(player)
		local ServerStorage = game:GetService("ServerStorage")
		ServerStorage.StarterCharacter:Clone().Parent = game.StarterPlayer
		player:LoadCharacter()
end)

Works! Thanks iOwn_You and BestCreativeBoy

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