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

How would I make a Tycoon Saving Progress and Money?

So I'm making a tycoon game with a bit of scripting experience. I do get that you can use Data Store but I really don't get it. It could be awesome if someone helped me out a bit. I'm using Zednov's Tycoon Kit for this tycoon game. If you need the scripts, just ask me please.

1 answer

If you use this code, You gotta try it in a real game

Add a script to server script service and call it dataHandler or whatever you want

Here's the code you use:

-- Adding variables like the data store service, myData store is your data store, Players left is to detect if there's amount of players to detect when the server can shutdown
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("RobloxDataStore")
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	-- every time a player joins it add 1 more to the players left
	
	playersLeft = playersLeft + 1
	
	-- Adding money to the play
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = leaderstats
	
	-- This adds data into the player data with userid
	
	local PlayerUserId = player.UserId
	
	-- Making a blank variable to store data
	
	local player_data
	
	-- Checks if it's successful or else you get an error message
	-- Makes a pcall so if there's an error it won't break the script
	
	local success, errorMessage = pcall(function()
		
		-- Sets player_data as players userid in your datastore
		
		player_data = myDataStore:GetAsync(PlayerUserId)
	end)
	
	-- If it's successful then the money will be what we saved
	
	if success then
		money.Value = player_data.Money
	end
end)

-- Makes a bindable event, Used to tell the script to do something when the game is shutting down

local bindableEvent = Instance.new("BindableEvent")

-- Fires what a player leaves the game

game.Players.PlayerRemoving:Connect(function(player)
	
	-- Gets the userid again
	
	local playerUserId = player.UserId
	
	-- The player_data you can save money is your money value
	
	local player_data = {
		Money = player.leaderstats.Money.Value
	}
	
	-- When a player leaves it will set your money to money in player data at the amount of money you have
	
	local success, errorMessage = pcall(function()
		myDataStore:SetAsync(playerUserId, player_data)
		
		-- Removes from players left
		
		playersLeft = playersLeft - 1
		
		-- Fires the bindable event
		
		bindableEvent:Fire()
	end)
	
	-- If it was successful then it prints saved to tell you that it was successful else it tells you what the problem is
	
	if success then
		print("Saved!")
	else
		warn(errorMessage)
	end
end)

-- If the players left is above 0 then the server is gonna wait until everyone's data was saved and then it can shut down

if playersLeft > 0 then
	bindableEvent.Event:Wait()
end

please read what I put the green things in, I tell you how it works

5 comments

0

All right, I will try it out and see if it works. Thank you so much.

0

So, I tried it out and all it does is make a new leaderstat from the one I already have.

0

please read what I put the green things in, I tell you how it works

0

Yes I did that but is there any way I can make it save money and save the tycoon? Not just adding a different value to the leaderboard.

0

If you want to save the tycoon, that would require saving a model to the datastore,,, i found this link, but it looks complicated: https://devforum.roblox.com/t/how-i-can-make-a-model-data-store/225105 . I am no expert but if you are making a regular tycoon where the player has to buy stuff and add it, maybe you can save what the player has bought so when they rejoin it has the info

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