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

attempt to index nil with 'HumanoidRootPart' How to fix?

Im trying to script a egg hatching system and im getting this error on line 55 of this script, how can i fix it?

local clickDetector = script.Parent

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataModule = require(ReplicatedStorage:WaitForChild("DataModule"))
local Pets = ReplicatedStorage:WaitForChild("Pets")

local function ChoosePet(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	local PetGui = PlayerGui.PetGui
	local EggIcon = PetGui.EggIcon
	local PetImages = PetGui.PetImages
	
	local timer = tick()
	
	EggIcon.Visible = true
	
	while tick() - timer < DataModule.EggOpeningLength do
		wait(.1)
		EggIcon.Rotation = math.random(-9,9)
	end
	
	local petNumber = math.random(1,100)
	local petFound = false
	local pet = nil
	while petFound == false do
		for i, v in pairs(DataModule.Pets) do
			if math.random(1,v) == petNumber then
				petFound = true
				pet = i
			end
		end
	end
	
	EggIcon.Visible = false
	
	local PetImage = PetImages:FindFirstChild(pet)
	PetImage.Visible = true
	wait(2)
	PetImage.Visible = false
	
	return pet
end

	
clickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.Money.Value >= DataModule.EggCost then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - DataModule.EggCost
		
		local petOpened = ChoosePet(player)
		
		print(petOpened)
			
		local pet = Pets:FindFirstChild(petOpened):clone()
		pet.Position = player.Character.HumanoidRootPart.Position
		pet.Parent = game.Workspace
	end
end)

1 comment

0

Try using player.Character.Humanoid.Torso.CFrame

1 answer

✓ Accepted answer

Your error says Character is nil, doesn't exists. So you must do a variable for the player's character, i recommend doing for example right after clickDetector.MouseClick:Connect(function(player) do:

local char = player.Character

And then do this in the line 55:

pet.Position = char.HumanoidRootPart.Position

Hope this works! If it doesn't, at least you know what's the error :D

1 comment

0

ohhhhh okay thanks

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