Hey there!
So I was making a DataStore called Money. I have created a little ‘test’ script that removes the balance from the player and gives the object requested. However, when I leave, it does not save! I believe it is because I need to use a RemoteEvent, but I’m not quite sure.
Here is my DataStore script:
local DSS = game:GetService('DataStoreService')
local DS = DSS:GetDataStore('MoneyStats')
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
money.Value = 0
local data = DS:GetAsync(Player.UserId)
if data then
money.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DS:SetAsync(Player.UserId, Player.leaderstats.Money.Value)
end)
And here is my test script (ignore all the sofa variables):
-- // Buttons
local buyblacksofa = script.Parent.BlackBuyButton
local buybluesofa = script.Parent.BlueBuyButton
local buygreensofa = script.Parent.GreenBuyButton
local buyorangesofa = script.Parent.OrangeBuyButton
local buyredsofa = script.Parent.RedBuyButton
local buyyellowsofa = script.Parent.YellowBuyButton
local player = game.Players.LocalPlayer
local buywhitesofa = script.Parent.WhiteBuyButton
local frame = script.Parent.Parent.Frame
local purchasefailed = script.Parent.Parent.PurchaseFailed
local purchasegranted = script.Parent.Parent.PurchaseGranted
-- // Assets
local blacksofa = game.ReplicatedStorage.Sofa.BlackSofa
local bluesofa = game.ReplicatedStorage.Sofa.BlueSofa
local greensofa = game.ReplicatedStorage.Sofa.GreenSofa
local orangesofa = game.ReplicatedStorage.Sofa.OrangeSofa
local redsofa = game.ReplicatedStorage.Sofa.RedSofa
local yellowsofa = game.ReplicatedStorage.Sofa.YellowSofa
local whitesofa = game.ReplicatedStorage.Sofa.WhiteSofa
-- // The main script
buyblacksofa.MouseButton1Click:Connect(function()
if player.leaderstats.Money.Value <= 500 then
purchasefailed.Visible = true
wait(5)
purchasefailed.Visible = false
elseif
player.leaderstats.Money.Value >= 500 then
blacksofa:Clone()
player.leaderstats.Money.Value = player.leaderstats.Money.Value -500
blacksofa.Parent = game.Workspace
blacksofa.Position = Vector3.new(-30, 0.5, -57)
purchasegranted.Text = "Successfully purchased a Black Sofa!"
purchasegranted.Visible = true
wait(5)
purchasegranted.Visible = false
end
end)
The DataStore script script is located inside ServerScriptService, and the test script is located inside my StarterGui (InsertTest.Frame).