local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local function saveData(player)
local tableToSave = {
player.leaderstats.Essence.Value;
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Essence = Instance.new("IntValue")
Essence.Name = "Essence"
Essence.Parent = leaderstats
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Essence.Value = data[1]
else
print("The player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved!")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
Script that gives the player currency after winning (if it’s important)
You can’t save datastore values on the client. Get the player object and save it from the server. If you really want it to run on the client, use a RemoteEvent to tell the server to save the value.
Also, why is the Values folder parented to the Workspace if they are numbervalues? It’s way more organised if you put it in ReplicatedStorage.
The reason the DataStore script isn’t saving the data when a player wins a round is because there is no code in the script that updates the value of Essence when the player wins.
You’ll need to modify the script that gives the player currency after winning to also update the value of Essence and save it to the DataStore.
Here’s an example of how you can modify the script to update Essence and save it to the DataStore:
game.Workspace.Values.CoinsValue.Changed:Connect(function(newValue)
if newValue == 10 then -- Change this to the value that triggers a win
local player = game.Players.LocalPlayer
player.leaderstats.Essence.Value = player.leaderstats.Essence.Value + 1 -- Change this to the amount of Essence the player should receive when they win
saveData(player) -- Call the saveData function to save the updated value to the DataStore
end
end)
Note that you’ll need to replace the 10 with the value that triggers a win in your game, and replace the 1 with the amount of Essence the player should receive when they win.
Also, make sure to add the saveData function to this script, or require the DataStore script that contains it.
Sure. You’re going to need a script and a LocalScript.
First, create a remoteEvent somewhere inside ReplicatedStorage
LocalScript:
-- we need to tell the server to change the player's leaderstat value
-- so, we will use a remote event to communicate this to the server.
local remote = game:GetService("ReplicatedStorage").RemoteEvent
remote:FireServer(currencychange)
-- Remember, when using the FireServer method, we don't need to specify the player argument as it is already sent for us!
Script
local remote = game:GetService("ReplicatedStorage").RemoteEvent
remote.OnServerEvent:Connect(function(player, currencychange)
player:WaitForChild("leaderstats").Essence.Value += currencychange
end)
-- this is when we recieve the message from the client saying that they want their essence value changed.
-- remember, when we recieve a message from the client, we need to get WHICH client
-- so, we need to pass the player parameter into the Connect Function part.
If you need me to explain a bit more, let me know!
it measn that the Essence.Value is nil.
Easy fix - we just check if its nil, and if it is, we set it to 0
local remote = game:GetService("ReplicatedStorage").RemoteEvent
remote.OnServerEvent:Connect(function(player, currencychange)
local essence = player:WaitForChild("leaderstats").Essence
if essence.Value == nil then
essence.Value = 0
essence.Value += currencychange
end)
-- this is when we recieve the message from the client saying that they want their essence value changed.
-- remember, when we recieve a message from the client, we need to get WHICH client
-- so, we need to pass the player parameter into the Connect Function part.
Or, its that you didn’t provide a value for CurrencyChange when you called :FireServer
yeah the reason why is because workspace.Values.CoinsValue.Value is most likely nil.
Also, you are meant to change Essence.Value by currency change, that why its passed by the remote event…
Here, you’re using LocalPlayer (I assume it’s LocalScript), so now whenever you do something in LocalScript, it doesb’t replicate to server and DataStores save it from server.