✓ Accepted answer
I am making a simple DataStore that saves you money in leaderstats. It isn’t saving for some reason but it is loading fine and I can’t work out why.
Here is the code:
Server Script in ServerScriptService:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("StatsSave")
local DefultData = {
Money = 100;
}
-- Load Data
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = leaderstats
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Money.Value = Data
else
Money.Value = DefultData.Money
end
end)
-- Save Data
local function SaveData(Player)
local Money = Remotes.MoneySave:InvokeClient(Player)
DataStore:SetAsync(Player.UserId, Money)
end
game.Players.PlayerRemoving:Connect(SaveData)
LocalScript in StarterPlayerScripts:
local leaderstats = script.Parent.Parent:WaitForChild("leaderstats")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local function SendData()
return leaderstats.Money.Value
end
Remotes.MoneySave.OnClientInvoke = SendData
I have a RemoteFunction in ReplicatedStorage in a folder called Remotes and the RemoteFunction is called MoneySave.
Note:I have Studio API Services turned on and I get no errors.
Thank you in advanced.
2 comments
Which part does not work, obtaining the value or saving the value? Also if the output throws any errors please add them aswell
i does not have an error but it wont work