local dss = game:GetService("DataStoreService")
local DataStoreToKeep = dss:GetDataStore("Data1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder") --making leaderstats
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new('IntValue')
money.Name = "Money$"
money.Value = 0
money.Parent = leaderstats
local data
local success, errorMessage = pcall(function()
data = DataStoreToKeep:GetAsync(player.UserId.."-money")
end)
if success then
money.Value = data
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
DataStoreToKeep:SetAsync(player.UserId.."-money", player.leaderstats["Money$"].Value)
end)
if success then
print("Saved Data")
else
warn(errorMessage)
end
end)
Why won’t this script save the currency the way I intended it in the beginning of the video??
Can you recreate this script and/or add something that will make it work with what I need please!
You need to add bindToClose as that triggers before PlayerRemoving does for the final player.
game:BindToClose(function()
for _, player in game.Players:GetPlayers() do
local success, errorMessage = pcall(function()
DataStoreToKeep:SetAsync(player.UserId.."-money", player.leaderstats["Money$"].Value)
end)
end
end)
How Exactly are you adding the values? If you are using a LocalScript, it wont save at all because the Effects will not Replicate to the Server, and Since the Server is not getting the Changes, it will think that the value hasn’t changed at all.
@Offpath, thats not his problem, Its not required to have it, as it is just primarly used as an emergency save in case the DataStore fails, which in this case it should work properly if he tested multiple times.
Rewrote the code to make it officially work, just reworded the code and some other small stuff…
Here’s the new code that should work. Feel free to reply to this if you are getting any problems!
--[[
You can remove this:
This script is in ServerScriptService as a regular script. If you use local scripts it will not save. No Idea what the problem
you were getting was about, I tested it and it worked! Hope this helps
]]
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Name") -- Change the name to whatever you want, if you change it with existing data on th ename, it will NOT carry over.
game.Players.PlayerAdded:Connect(function(player)
local Data = DataStore:GetAsync("Player_"..player.UserId)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new('IntValue', leaderstats)
Money.Name = "Money"
Money.Value = 0
if Data then
Money.Value = Data["Money"]
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local Data = {
Money = player.leaderstats.Money.Value;
--Add any other variables you want!
}
local success, err = pcall(function()
DataStore:SetAsync("Player_"..player.UserId, Data)
end)
if err then
warn("There was an error saving data:", err)
end
end)