Hello fellow devforum users! I need to make a world system for my simulator game(don’t judge) and I can’t seem to figure out how. I am aware how to teleport people and all that, the datastore part is the one that is confusing me. If you can tell me how to go about that or send a tutorial it would be great! All help is appreciated!
If this is in the wrong category please correct me.
Here’s a quick simple tutorial on how to use DataStores:
-- When working with datastores, you must be using a script on the *server* --
local DSS = game:GetService("DataStoreService"); -- Get the DataStore Service
local MoneyDS = DSS:GetDataStore('Money', <optional scope>); -- "Money" DataStore for example
--[[ Note: If you want to implement leaderboards, you can use :GetOrderedDataStore
instead (only if you're storing numbers) ]]--
-- Function for loading data. You can do this when the player joins the game.
local function LoadPlayerData(Player:Player)
local Data = MoneyDS:GetAsync(Player.UserId); -- Gets the data
if not Data then
Data = 0; -- If the player doesn't have any data, set it to default (ex 0)
end;
return Data;
end;
-- Save player data.
local function SavePlayerData(Player:Player, Money:number)
local Succeeded, ErrorMessage = pcall(function() -- Wrap in pcall for error handling
MoneyDS:SetAsync(Player.UserId, Money) -- Store the data
end);
if not Succeeded then -- If there was an error with storing the data
warn(ErrorMessage); -- Print the error
end;
return Succeeded;
end;
-- Now for actually loading the data..
game.Players.PlayerAdded:Connect(function(Player)
local PlayerData = LoadPlayerData(Player); -- gets the players data
Player:SetAttribute("Money", PlayerData) -- Set as attribute to player.
-- You can also create a NumberValue, or implement leaderstats.
end);
-- Save data when player leaves
game.Players.PlayerRemoving:Connect(function(Player)
local Success = SavePlayerData(Player, Player:GetAttribute("Money"));
-- Save the data.
end);
This is a very basic example of how to use DataStores
If you have any more questions, or if I didn’t resolve your issue, feel free to reply