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

Why does this script work in studio but not online? It has errors online too

I am not sure why this happens. Here are the errors I get.

Attempt to index local player is what I see in the dev console

Tool = script.Parent
plr = game.Players.LocalPlayer.Character
function functionname()

local name = plr.Name
	local playerinplayers= game.Players:FindFirstChild(plr.Name)
	if 	playerinplayers ~= nil then
	playerinplayers.CanLoadCharacterAppearance = false 
	plr.Head.face.Texture = "rbxassetid://28257733"
	local bodycolors = Instance.new("BodyColors",plr)
	bodycolors.RightArmColor = BrickColor.new("Pastel brown")
bodycolors.RightArmColor = BrickColor.new("Pastel brown")
bodycolors.LeftArmColor = BrickColor.new("Pastel brown")
bodycolors.RightLegColor = BrickColor.new("Pastel brown")
bodycolors.LeftLegColor = BrickColor.new("Pastel brown")
bodycolors.TorsoColor = BrickColor.new("Pastel brown")
bodycolors.HeadColor = BrickColor.new("Pastel brown")
game.ReplicatedStorage["Roblox 2.0 Left Arm"]:Clone().Parent = plr
wait()
game.ReplicatedStorage["Roblox 2.0 Left Leg"]:Clone().Parent = plr
wait()
game.ReplicatedStorage["Roblox 2.0 Right Arm"]:Clone().Parent = plr
wait()
game.ReplicatedStorage["Roblox 2.0 Right Leg"]:Clone().Parent = plr
wait()
game.ReplicatedStorage["Roblox 2.0 torso"]:Clone().Parent = plr
wait()

plr.Shirt:Destroy()
wait()
plr.Pants:Destroy()	
local shirt = Instance.new('Shirt', plr)
 shirt.ShirtTemplate = 'rbxassetid://153667173'
 local pants = Instance.new('Pants', plr)
 pants.PantsTemplate = 'rbxassetid://115544973'

for _,v in pairs(plr:GetChildren()) do
if v.ClassName == "Hat" then
v:Destroy()
wait()
game.ReplicatedStorage.GrayHair:clone().Parent = plr
wait()
game.ReplicatedStorage.GrayHair2:clone().Parent = plr

end
end
	end
	end
Tool.MouseButton1Click:connect(functionname) 

2 answers

✓ Accepted answer

There was some things that were wrong with your code

1. Use char as a variable to get character

2. Use 'WaitForChild' to find items

plr = game.Players.LocalPlayer
Char = plr.CharacterAdded:wait()
Head = Char:WaitForChild('Head')
Face = Head:WaitForChild('face')
BodyColor = Char:WaitForChild('Body Colors')

	function onButtonClicked()
--// Changes face
		if Face.Texture ~= "rbxassetid://28257733" then
	Face.Texture = "rbxassetid://28257733"
--// Changes player's Body Color
		BodyColor.RightArmColor = BrickColor.new("Pastel brown")
		BodyColor.RightArmColor = BrickColor.new("Pastel brown")
		BodyColor.LeftArmColor = BrickColor.new("Pastel brown")
		BodyColor.RightLegColor = BrickColor.new("Pastel brown")
		BodyColor.LeftLegColor = BrickColor.new("Pastel brown")
		BodyColor.TorsoColor = BrickColor.new("Pastel brown")
		BodyColor.HeadColor = BrickColor.new("Pastel brown")
	
--// Clones CharacterMesh in player
	game.ReplicatedStorage.CharMesh["Roblox 2.0 Left Arm"]:Clone().Parent = Char
	game.ReplicatedStorage.CharMesh["Roblox 2.0 Left Leg"]:Clone().Parent = Char
	game.ReplicatedStorage.CharMesh["Roblox 2.0 Right Arm"]:Clone().Parent = Char
	game.ReplicatedStorage.CharMesh["Roblox 2.0 Right Leg"]:Clone().Parent = Char
	game.ReplicatedStorage.CharMesh["Roblox 2.0 torso"]:Clone().Parent = Char
--// Finds Shirt
		if Char:FindFirstChild('Shirt') then
			Char:FindFirstChild('Shirt'):Destroy()
--// Finds Pants		
		if Char:FindFirstChild('Pants') then
			Char:FindFirstChild('Pants'):Destroy()
	
--// Creates shirt if nil
		if Char:FindFirstChild('Shirt') == nil then
		local shirt = Instance.new('Shirt', Char)
			shirt.ShirtTemplate = 'rbxassetid://153667173'
	
--// Creates pants if nil
		if Char:FindFirstChild('Pants') == nil then
	local pants = Instance.new('Pants', Char)
		pants.PantsTemplate = 'rbxassetid://115544973'
--// remove hats

		for i,v in pairs(Char:GetChildren()) do
			if (v.ClassName == "Hat") then
		v:Destroy()
--// Clones hats in player
	
		if Char:FindFirstChild('GrayHair') == nil and Char:FindFirstChild('GrayHair2') == nil then
			game.ReplicatedStorage.Hats.GrayHair:clone().Parent = Char
			game.ReplicatedStorage.Hats.GrayHair2:clone().Parent = Char

	end end end end end end end end end
script.Parent.MouseButton1Click:connect(onButtonClicked)

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