Help me save and load tycoon using ProfileService

I want to save and load my tycoon, since i have never saved objects/models before i have a small knowledge on how to do that.

For anyone wondering about profileservice script, its a default from their website

-- ProfileTemplate table is what empty profiles will default to.
-- Updating the template will not include missing template values
--   in existing player profiles!
local ProfileTemplate = {
	Droppers = {},
	hitButtons = {},
}

----- Loaded Modules -----

local ProfileService = require(game.ServerScriptService.Data.profile.ProfileService)

----- Private Variables -----

local Players = game:GetService("Players")

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

local Profiles = {} -- [player] = profile

----- Private Functions -----


local function DoSomethingWithALoadedProfile(player, profile)
	
	
end
	


local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile ~= nil then
		profile:AddUserId(player.UserId) -- GDPR compliance
		profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
		profile:ListenToRelease(function()
			Profiles[player] = nil
			-- The profile could've been loaded on another Roblox server:
			player:Kick()
		end)
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
			-- A profile has been successfully loaded:
			DoSomethingWithALoadedProfile(player, profile)
		else
			-- Player left before the profile loaded:
			profile:Release()
		end
	else
		-- The profile couldn't be loaded possibly due to other
		--   Roblox servers trying to load this profile at the same time:
		player:Kick() 
	end
end

----- Initialize -----

-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end

----- Connections -----

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = Profiles[player]
	if profile ~= nil then
		profile:Release()
	end
end)

And here i made a script in button to spawn a dropper

local touchPart = script.Parent
local dropper1 = game.ServerStorage.Droppers.dropper1

touchPart.Touched:Connect(function(hit)
	local character = hit.Parent
	local characterHumanoid = character:FindFirstChildOfClass("Humanoid")
	if character and characterHumanoid then
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			touchPart.Parent.Parent = game.ServerStorage.hitButtons
			local dropperClone = dropper1:Clone()
			dropper1.Parent = workspace
		end
	end
end)

Before working with ProfileService you should probably know how to work with parts with a regular datastore
How To SAVE Objects With DataStores | Roblox Studio Tutorial (youtube.com)

1 Like

The player data will need to contain a table of all the unlocked model names/IDs within the player’s tycoon. You can’t actually save instances within a data store, you could save unlocked models via the name or some ID that you assign to each model. When the player joins the game, you can loop through the table of unlocked models they have and put them inside the tycoon.

1 Like

where should i make that table? can you explain more?

local TableProfile = {
	Tycoon = {
		Level1 = {
		},
		Level2 = {
			
		},
		Level3= {
		}
	},
	AnythingElse = 500,
	Cash = 100000000
}

profile.Data.TycoonSection["any section"] = TableProfile["Tycoon"] -- Table
profile.Data.Cash += TableProfile["Tycoon"]["Cash"] -- save cash

But Im pretty sure you need a huge template or leaderstats to use that. It still works without them, but it might cause an error for new players if I’m not mistaken

The player data table that you save must contain all of that tycoon data. Remember that you must store the tycoon instances as either IDs or names that you can reference to the actual models within the game and load them in accordingly.

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