Droppers stop working after rejoining

I made a tycoon (not complete yet)
my problem is that when I join for the first time it’s working fine after buying the dropper and conveyer and then rejoining the game it stops working until I change data store name (resetting it)
these are the scripts that I have:
PlotHandler:

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)

Data:

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

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

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerData2",
	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)

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

I think you forgot to make it activate after it’s loading

1 Like

ooooooooooooooh ty so much
I was trying to figure it out for hours xD
I’m wacky sometimes!

1 Like

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