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.
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
Closed as Not Constructive by 2_MMZ, pwx, and Amiaa16
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?