Attempt to index nil with PrimaryPart morphchanger

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the player with access to the spy tool be able to change their character to a character of a different player in the server

  2. What is the issue? Include screenshots / videos if possible!
    I get this error

17:23:34.235 ServerScriptService.ToolServerSided.SpyToolServer:9: attempt to index nil with ‘PrimaryPart’ - Studio
17:23:34.235 Stack Begin - Studio
17:23:34.235 Script ‘ServerScriptService.ToolServerSided.SpyToolServer’, Line 9 - Studio
17:23:34.235 Stack End - Studio

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Didnt find anything on the devforum and chatgpt didnt give me the answer to the problem

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Server:

game.ReplicatedStorage.RandomRemotesFolder.SpytoolMorphChanger.OnServerEvent:Connect(function(Player, localplayer)
	local oldCharacter = localplayer.Character
	local morphModel = Player.Character

	if morphModel then
		local newCharacter = morphModel:Clone()


		if oldCharacter.PrimaryPart and newCharacter.PrimaryPart then
			newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
			localplayer.Character = newCharacter
			newCharacter.Parent = workspace
		else
			warn("primaryPart is missing")
		end
	end
end)

client:

local SpyToolGui = script.Parent.SpyToolGui
local localplayer = game.Players.LocalPlayer


script.Parent.Equipped:Connect(function()
	local SpyGuiClone = SpyToolGui:Clone()
	SpyGuiClone.Parent = game.Players.LocalPlayer.PlayerGui
	local PlayerFrameButton = SpyGuiClone.MainSpyToolFrame.PlayerList.PlayerFrameButton
	
	
	local function UpdatePlayerList()
		for _, Player in ipairs(game.Players:GetChildren()) do
			local ClonedPlayerButton = PlayerFrameButton:Clone()
			ClonedPlayerButton.Name = Player.Name
			ClonedPlayerButton.Parent = PlayerFrameButton.Parent
			ClonedPlayerButton.Visible = true
			
			ClonedPlayerButton.PlayerProfilePicture.Image = "rbxthumb://type=AvatarHeadShot&id=" .. Player.UserId .. "&w=150&h=150"
			
			if Player.Team then
				ClonedPlayerButton.PlayerNameAndTeamText.Text = Player.Name .. " | " .. Player.Team.Name
			else
				ClonedPlayerButton.PlayerNameAndTeamText.Text = Player.Name .. " | " .. "Team = nil"
			end
			
			ClonedPlayerButton.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.RandomRemotesFolder.SpytoolMorphChanger:FireServer(Player, localplayer)
			end)
		end
	end
	
	UpdatePlayerList()
	
	script.Parent.Unequipped:Connect(function()
		SpyGuiClone.Enabled = false
	end)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The Model newCharacter properties PrimaryPart would need to be set to a part within the Model.
Go to the properties of the Model and press on PrimaryPart, then select the part you want as the PrimaryPart… If you haven’t set that.

Via script; Model.PrimaryPart =

Its like that even if i metion HumanoidRootPart it still says the same error with HumanoidRootPart and i thought that it would be alright to just use PrimaryPart instead since that works with no problem for other morph stuff i can actually give you that script

MainMenuMorphRemotes.TeamsRemotes.SWATTeamRemoteEvent.OnServerEvent:Connect(function(player, teamName, group)
	if player:IsInGroup(group) then
		
		
		player.Team = teamName

		local rankInGroup = player:GetRankInGroup(34612531)
		local oldCharacter = player.Character
		local morphModels = {
			[66] = game.ServerStorage.Morphs.MainArmy.NavyHICOM["Grand Admiral"],
			[65] = game.ServerStorage.Morphs.MainArmy.NavyHICOM["Fleet Admiral"],
			[64] = game.ServerStorage.Morphs.MainArmy.NavyHICOM["Admiral"],
			[63] = game.ServerStorage.Morphs.MainArmy.NavyHICOM["Vice Admiral"],
			[53] = game.ServerStorage.Morphs.MainArmy.NavyLR["Officer"],
			[1] = game.ServerStorage.Morphs.MainArmy.Army["Trooper"]
		}

		local morphModel = morphModels[rankInGroup] 
		if rankInGroup < 52 then
			morphModel = game.ServerStorage.Morphs.MainArmy.Army["Trooper"]
		elseif rankInGroup >= 53 and rankInGroup <= 63 then
			morphModel = game.ServerStorage.Morphs.MainArmy.NavyLR["Officer"]
		end


		if morphModel then
			local newCharacter = morphModel:Clone()
			local playerface = oldCharacter.Head.face:Clone()
			playerface.Parent = newCharacter.Head
			newCharacter.Name = player.Name
			newCharacter.HumanoidRootPart.Anchored = false
			newCharacter.HumanoidRootPart.Anchored = false
			newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
			for i, v in ipairs(oldCharacter:GetChildren()) do
				if (v:IsA("Script") or v:IsA("LocalScript")) and v.Name ~= "BlockyAvatarScript" then
					v:Clone().Parent = newCharacter
				end
			end
			player.Character = newCharacter
			newCharacter.Parent = workspace
			for i, v in game.ReplicatedStorage.PlayersCharacter:GetChildren() do
				if v.Name == player.Name then
					v:Destroy()
				end
			end
			
			newCharacter:Clone().Parent = game.ReplicatedStorage.PlayersCharacter
			TeleportPlayerOnceTeamChoosen(player, player.Character)
		else
			-- Keep the player's original character if no morph model is found
			player.Character = oldCharacter
			for i, v in game.ReplicatedStorage.PlayersCharacter:GetChildren() do
				if v.Name == player.Name then
					v:Destroy()
				end
			end
			
			TeleportPlayerOnceTeamChoosen(player, player.Character)
			player.Character:Clone().Parent = game.ReplicatedStorage.PlayersCharacter
		end
		
	end
end)

Fixed it by just making it so that when a player joins the game their character gets cloned and put into a folder in ReplicatedStorage and then looking for the player with their name inside the folder and cloning that

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