local CurrentPlayer
local DataModule = require(game.ReplicatedStorage.DataModule)
game.Players.PlayerAdded:Connect(function(player) --// If player joins.
local value = Instance.new("IntValue") --// Creates the value.
value.Parent = player --// Sends it to the Player's Profile.
value.Name = "Coins" --// Names the Value.
CurrentPlayer = player
DataModule.SaveData(player, value) --// Loads the data; "player" is the "Player"; value is the "Value".
end)
game.Players.PlayerRemoving:Connect(function(player) --// If player left.
DataModule.LoadData(player, player.Coins) --// Saves the data; "player" is the "Player"; "player.Coins" is the "Value" we added before.
end)
game:BindToClose(function() --// Check if server shutdowns
DataModule.BindToClose(CurrentPlayer.Coins) --// Save the data.
end)
You can save 2 or more datas in just a script!
NOTE: You can add the module anywhere as long as you can find it by script.
–// Testing video using BindToClose
Changelog
v0.1:
Fixed BindToClose not saving data.
Added Datastore support up to 10 datastores in one script.
Can i ask a question? isn’t the player supposed to save data when leaving and load data when joining? (the words kinda mess me up a little)
Also is this going to work with game:BindToClose(function() ?
No, say if there was one player in the server, when he leaves the server gets shutted down, that means PlayerRemoving won’t fire, that’s why people use game:BindToClose(function(),
+When a game gets their server shutted down, the players’ data won’t be saved without game:BindToClose(function()
why require the module every time when you could just make a variable for it once
local DataModule = require(game.ReplicatedStorage.DataModule)
game.Players.PlayerAdded:Connect(function(player) --// If player joins.
local value = Instance.new("IntValue") --// Creates the value.
value.Parent = player --// Sends it to the Player's Profile.
value.Name = "Coins" --// Names the Value.
DataModule.SaveData(player, value) --// Saves the data; "player" is the "Player"; value is the "Value".
end)
game.Players.PlayerRemoving:Connect(function(player) --// If player left.
DataModule.LoadData(player, player.Coins) --// Load the data; "player" is the "Player"; "player.Coins" is the "Value" we added before.
end)
game:BindToClose(function() --// Check if server shutdowns
DataModule.BindToClose(CurrentPlayer.Coins) --// Save the data.
end)
ProfileService, DS2 and some other datastores are actually pretty difficult to understand to most new scripters, and naturestore’s way of saving data (talking about IncrementData) is something not everyone enjoys using
I feel like this module is great for new scripters to get started with saving data as roblox’s default datastore is hard to understand for most.
Those modules are for experienced developers that knows how to use it. Mine is for new developers. I’ll try my best to make it better so no data loss occurs.