Not spawning every part in tycoon

Hi
I’m about to make a tycoon and I’m in the middle of data store scripting but I found some problems


this is the thing that you should see when you join the game after buying droppers
but I’m seeing this

this is my data store script:

local ProfileService = require(script:WaitForChild('ProfileService'))

local ProfileTemplate = {
	cash = 10;
	Items = {};
}

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerData",
	ProfileTemplate
)

local Profiles = {}

local function playerAdded(player)
	local Profile = ProfileStore:LoadProfileAsync("Player"..player.UserId)

	if Profile then
		Profile:AddUserId(player.UserId)
		Profile:Reconcile()

		Profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)

		if player:IsDescendantOf(game.Players) == true then
			Profiles[player] = Profile
		else
			Profile:Release()
		end
	else
		player:Kick()
	end
end

game.Players.PlayerAdded:Connect(function(player)	
	
	playerAdded(player)
	
	local Profile = Profiles[player]
	if not Profile then warn('No Profile!') return end
	
	
	local folder = Instance.new('Folder')
	folder.Name = 'leaderstats'
	folder.Parent = player
	
	local cash = Instance.new('IntValue')
	cash.Name = 'cash'
	cash.Value = Profile.Data.cash or 10
	cash.Parent = folder
	
	cash:GetPropertyChangedSignal('Value'):Connect(function()
		Profile.Data.cash = cash.Value
	end)
	
	game.ServerScriptService:WaitForChild('PlotHandler'):WaitForChild('CreatePlot'):Fire(player, Profile.Data.Items)
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Profile = Profiles[player]
	
	if Profile then
		Profile:Release()
	end
end)

for _, player in game.Players:GetPlayers() do
	task.spawn(playerAdded, player)
end

script:WaitForChild('ItemUnlocked').Event:Connect(function(player, ItemId)
	local Profile = Profiles[player]
	table.insert(Profile.Data.Items, ItemId)
end)

and this one is my plot handler:

local Plots = game.Workspace.Plots
local TemplatePlot = game.Workspace.TemplatePlot

local function getPlot(player)
	for _, plot in Plots:GetChildren() do
		local owner = plot:GetAttribute('Owner')
		if not owner then continue end
		if owner ~= player.UserId then continue end
		return plot
	end
end

local function getItemFromTemplatePlot(itemId)
	for _, item in TemplatePlot.Items:GetChildren() do
		if item:GetAttribute('Id') == itemId	then
			return item
		end
	end
end

local function LoadItems(player, itemIdsTable)
	local plot = getPlot(player)

	for _, itemId in itemIdsTable do
		local item = getItemFromTemplatePlot(itemId)
		if not item then continue end
		
		local itemClone = item:Clone()
		local itemCFrame

		if itemClone:IsA('Model') then
			itemCFrame = itemClone:GetPivot()
		elseif itemClone:IsA('BasePart') then
			itemCFrame = itemClone.CFrame
		end

		local reletiveItemCFrame = TemplatePlot.CFrame:ToObjectSpace(itemCFrame)
		local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(reletiveItemCFrame)
		if itemClone:IsA('Model') then
			itemClone:PivotTo(worldCFrameOfNewPlot)
		elseif itemClone:IsA('BasePart') then
			itemClone.CFrame = worldCFrameOfNewPlot
		end
		
		itemClone.Parent = plot.Items
	end
end

script:WaitForChild('CreatePlot').Event:Connect(function(player, itemIdsTable)
	for _, plot in Plots:GetChildren() do
		if plot:GetAttribute("Taken") then continue end
		plot:SetAttribute('Taken', true)
		plot:SetAttribute('Owner', player.UserId)
		
		print('Plot Has been given too '..player.Name..'!')
		
		local ItemsFolder = Instance.new('Folder')
		ItemsFolder.Name = 'Items'
		ItemsFolder.Parent = plot
		
		local TemplateButtons = TemplatePlot.Buttons:Clone()
		local TemplateItems = TemplatePlot.Items
		
		LoadItems(player, itemIdsTable)
		
		for _, Button in TemplateButtons:GetChildren() do
			if table.find(itemIdsTable, Button:GetAttribute('IdOfItemToUnlock')) then continue end
			local ReletiveCFrame = TemplatePlot.CFrame:ToObjectSpace(Button:GetPivot())
			Button:PivotTo(plot.CFrame:ToWorldSpace(ReletiveCFrame))
			
			Button.buttonhead.Touched:Connect(function(hit)
				local player = game.Players:GetPlayerFromCharacter(hit.parent)
				if not player then return end
				if plot:GetAttribute('Owner') ~= player.UserId then return end
				
				if Button:GetAttribute('Debounce') then return end
				
				Button:SetAttribute('Debounce', true)
				
				task.wait(2, function()
					if Button then
						Button:SetAttribute('Debounce', false)
					end
				end)
				
				local itemToUnlockId = Button:GetAttribute('IdOfItemToUnlock')
				if not itemToUnlockId then warn('You forgot to add an IdOfItemToUnlock attribute') return end
				
				local Price = Button:GetAttribute('Price')
				
				if Price then
					if player.leaderstats.cash.Value < Price then
						warn('You Cant Afford This Item!')
						return
					end
					
					player.leaderstats.cash.Value -= Price
				end
				
				for _, item in TemplateItems:GetChildren() do
					if item:GetAttribute('Id') ~= itemToUnlockId then continue end
					local itemClone = item:Clone()
					local itemCFrame
					
					if itemClone:IsA('Model') then
						itemCFrame = itemClone:GetPivot()
					elseif itemClone:IsA('BasePart') then
						itemCFrame = itemClone.CFrame
					end
					
					local reletiveItemCFrame = TemplatePlot.CFrame:ToObjectSpace(itemCFrame)
					local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(reletiveItemCFrame)
					if itemClone:IsA('Model') then
						itemClone:PivotTo(worldCFrameOfNewPlot)
					elseif itemClone:IsA('BasePart') then
						itemClone.CFrame = worldCFrameOfNewPlot
					end
					
					for _, scriptObject in itemClone:GetDescendants() do
						if scriptObject:IsA('BaseScript') then
							scriptObject.Enabled = true
						end
					end
					
					itemClone.Parent = ItemsFolder
					Button:Destroy()
					game.ServerScriptService.Data.ItemUnlocked:Fire(player, itemToUnlockId)
				end
			end)
			
			Button.Parent = TemplateButtons
		end
		
		TemplateButtons.Parent = plot
		break
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for _, plot in Plots:GetChildren() do
		if not plot:GetAttribute('Owner') then continue end
		if plot:GetAttribute('Owner') ~= player.UserId then continue end
		plot:SetAttribute('Taken', nil)
		plot:SetAttribute('Owner', nil)
		
		print('Plot Has been removed from '..player.Name..'!')
		break
	end
end)

can anybody help?
I’m not that professional in scripting

Is the item id that you saved the same to one you have in TemplateItems?

yeah they are the same
I think the problem is I have 2 parts (barrel (the barrel is the conveyor) and dropper) with the same ID it was working before the data store but after that, it broke

1 Like

the system is only loading one
image

1 Like

so as I said I’m a new programmer
how should I load multiple?

1 Like

you should put all needed parts into one model under one id so the entire thing gets loaded

1 Like

I’ve figured out a nother way to fix it but I found a new problem when I join the game again everything stops working when I change the data store it will start working again and after rejoining stops again …
CollectorScript:

script.Parent.Touched:Connect(function(hit)
	if hit:GetAttribute('CashToGive') then
		local Plot = script.Parent.Parent.Parent.Parent
		if not Plot then warn('No Plot') return end
		
		local plotOwnerUserId = Plot:GetAttribute('Owner')
		
		if not plotOwnerUserId then warn('No Plot Owner User Id') return end
		
		hit:Destroy()
		
		local PlayerObject = game.Players:GetPlayerByUserId(plotOwnerUserId)
		PlayerObject.leaderstats.cash.Value += hit:GetAttribute('CashToGive')
	end
end)

DropperScript:

local DropperTemplate = game.ServerStorage:WaitForChild('DropperTemplate')

while task.wait(2) do
	local NewDropperPart = DropperTemplate:Clone()
	NewDropperPart.CFrame = script.Parent.Drop.CFrame
	NewDropperPart:SetAttribute('CashToGive', script.Parent:GetAttribute('CashPerDrop'))
	NewDropperPart.Parent = script.Parent.DropperParts
end

did I change anything by mistake and cant find it or what???

1 Like

you should destroy the hit part after getting its attribute

actually, does the dropperscript happen to prematurely exist in the dropper?

nothing changed still not dropping anything