[SOLVED] ServerScriptService.Script:43: attempt to index nil with 'GetPivot'

I’m trying to make a tycoon and for some reason its telling me “ServerScriptService.Script:43: attempt to index nil with ‘GetPivot’”

Just wondering whats wrong with it

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Items = ReplicatedStorage:WaitForChild('Items')
local Tycoons = game.Workspace:WaitForChild("Tycoons")

local PlaceholderTycoon = ReplicatedStorage:WaitForChild('Tycoon')

-- clear out all items from tycoons
for _, Tycoon in CollectionService:GetTagged('Tycoon') do
	if Tycoon:isDescendantOf(ReplicatedStorage) then continue end
	Tycoon.Items:ClearAllChildren()
end

local function getItemInTycoonById(itemId)
	for _, Item in PlaceholderTycoon.Items:GetChildren() do
		if Items:GetAttribute('Id') == itemId then
			return Item
		end
	end
end

local function getItem(itemId)
	for _, Item in Items:GetChildren() do
		if Item:GetAttribute('Id') == itemId then
			return Item
		end
	end
end

local function assignTycoon(player)
	for _, Tycoon in Tycoons:GetChildren() do
		if Tycoon:GetAttribute("Taken") then continue end
		Tycoon:SetAttribute("Taken", true)
		Tycoon:SetAttribute("UserId", player.UserId)
		return Tycoon
	end
end

local function getRelativeCFrame(itemId)
	local Item = getItemInTycoonById(itemId)
	
	local relativeCF = PlaceholderTycoon.PrimaryPart.CFrame:toObjectSpace(Item:GetPivot())
	return relativeCF
end

local function getTycoon(player)
	for _, tycoon in game.Workspace.Tycoons:GetChildren() do
		if tycoon:GetAttribute('UserId') == player.UserId then
			return tycoon
		end
	end
end

local function unlockItem(player, itemId)
	local tycoon = getTycoon(player)
	if not tycoon then return end
	
	local Item = getItem(itemId):Clone()
	local Cost = Item:GetAttribute('Cost')
	
	if player.leaderstats.Cash.Value < Cost then
		return false
	end
	
	player.leaderstats.Cash.Value -= Cost
	
	local RelativeCF = getRelativeCFrame(itemId) -- CF of the Item realtive to the Tycoon
	-- how far away from the tycoon is the item? object space (10, 0, 2)
	
	local WorldCF = tycoon.Primarypart.CFrame:toWorldSpace(RelativeCF)
	
	Item:PivotTo(WorldCF)
	Item.Parent = tycoon.Items
	
	return true
end

for _, Button in CollectionService:GetTagged('Button') do
	local ItemId = Button:GetAttribute('ItemId')
	local Item = getItem(ItemId)
	Button.BillboardGui.TextLabel.Text = getItem(Button:GetAttribute('ItemId')).Name.." - "..Item:GetAttribute('Cost')
	
	Button.Touched:Connect(function(hit)
		if Button:GetAttribute('Unlocked') then return end
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player then return end
		
		local tycoon = getTycoon(player)
		if not tycoon then return end
		
		if not Button:IsDescendantOf(tycoon) then return end
		
		local success = unlockItem(player, Button:GetAttribute('ItemId'))
		if not success then return end
		
		Button.Transparency = 1
		Button.BillboardGui.Enabled = false
		Button:SetAttribute('Unlocked', true)
	end)
	
end

game.Players.PlayerAdded:Connect(function(player)
	local Tycoon = assignTycoon(player)
	if not Tycoon then warn("Could not assign tycoon to "..player.Name) return end
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Tycoon= getTycoon(player)
	Tycoon.Items:ClearAllChildren()
	
	for _, Button in Tycoon.Buttons:GetChildren() do
		Button.Transparency = 0
		Button.BillboardGui.Enabled = true
	end
end)

local relativeCF = PlaceholderTycoon.PrimaryPart.CFrame:toObjectSpace(Item:GetPivot())
return relativeCF
end

1 Like

Please move this topic to Scripting Support. Maybe you just misclicked this topic and put it in the wrong place.

Thank you!

1 Like

Oh thanks, I didn’t even notice

1 Like

Figured it out no worries. Took me a while

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