'attempt to index nil with 'StarterCharacter' Bug

Hello developers!
I would like help with a script i am currenctly making, please help!

  1. What do you want to achieve? Keep it simple and clear!

I would like to achieve that when the player spawns/reset, a script will replace their model with a skin.

  1. What is the issue? Include screenshots / videos if possible!

The issue is, everything works fine it knows what skin to choose but it gives me a error

Workspace.masterminyxs.Script:8: attempt to index nil with 'StarterCharacter'


image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Nope, i’ve tried to rescript it in a way but it couldn’t do anything, and as for devforum i couldn’t find anything.

This is the script from the code, if anybody finds something odd or knows the reason, please tell!

local db = true
if db == true then
	db = false
	local hit = script.Parent
	local plr = game.Players:GetPlayerFromCharacter(hit)
	local skin = plr:FindFirstChild("SelectedSkin"):FindFirstChild("YourSkin")
	local orign = game.ServerStorage.Skins:FindFirstChild(skin.Value)
	local model = orign.StarterCharacter
	local oldModel = plr.Character
	local newModel = model:Clone()

	local oldCFrame = oldModel:GetPrimaryPartCFrame()

	plr.Character = newModel
	newModel.Parent = workspace -- [1] this follows current character loading behavior
	newModel:SetPrimaryPartCFrame(oldCFrame)
	newModel.Animate.Disabled = true
	newModel.Animate.Disabled = false
end

This script is a normal script located in StarterCharacterScripts

2 Likes

From the error message I know that orign = nil. The only way for this to be true means that FindFirstChild(skin.Value) is returning nil, so skin.Value is not the name of anything in the Skins folder, you could set up a print statement to find out what it actually is, and then if that isn’t able to help you solve the problem, you could give more information.

1 Like

Hello, here is my solution. If it doesn’t work, or you have any questions please let me know.

local isLocalDB = true

if isLocalDB then
	isLocalDB = false

	local hit = script.Parent
	local player = game.Players:GetPlayerFromCharacter(hit)
	local selectedSkin = player.Character:FindFirstChild("SelectedSkin")
	local yourSkin = selectedSkin and selectedSkin:FindFirstChild("YourSkin")

	if yourSkin then
		local original = game.ServerStorage.Skins:FindFirstChild(yourSkin.Value)

		if original then
			local newModel = original.StarterCharacter:Clone()
			local oldModel = player.Character
			local oldCFrame = oldModel:GetPrimaryPartCFrame()

			player.Character = newModel
			newModel.Parent = workspace
			newModel:SetPrimaryPartCFrame(oldCFrame)
			newModel.Animate.Disabled = true
			newModel.Animate.Disabled = false
		end
	end
end
2 Likes

Thanks, just added a wait & removed character on

local selectedSkin = player.Character:FindFirstChild("SelectedSkin")
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.