Hi. I was making a shop, It works, but the save dose not. This system works by clicking a button which takes away money,(you cant press the button again), A button is added into the inventory.
but the saving part dose not work!
When I re join the game, It makes me re-buy the Item and it is not in the inventory. My money saves, but The money I loose from the purchase dose not. EX- I have 50 currency, i buy the item, I have 30 currency, I rejoin, I need to buy the item again, and I have 50 currency.
Here is my datastore
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("MoneySaveSystem")
game.Players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local money = Instance.new("IntValue", folder)
money.Name = "Money"
money.Value = ds1:GetAsync(plr.UserId) or 0
ds1:SetAsync(plr.UserId, money.Value)
money.Changed:connect(function()
ds1:SetAsync(plr.UserId, money.Value)
end)
end)
If anyone could help me and tell me if I need to change/add my datastore, that would really help.
Do you have a player leaving event or bind to close?
Sorry this doesn’t matter since it updates your data when money changes. May I ask are you changing the money on the client side, if so I don’t think the Changed Event would run.
This is the script that is inside of the buy button if that answers any questions
local item = script.Parent
local Shoes = script.Parent.Parent.Parent.Frame1.Shoes
local value = 40000
item.MouseButton1Click:Connect(function()
if game.Players.LocalPlayer.leaderstats.Money.Value >= value then
game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value - value
item.Text = "purchased"
Shoes.Visible = true
value = 0
else
item.Text = "not purchased"
wait(1)
item.Text = "Purchase"
value = 0
Shoes.Visible = true
end
end)
Why would you save the money every time the money changes? It’s going to request too much saving and it will overflow and exceed the limit. You should only save the data when the player leaves, and you should wrap the calls in pcall() function.
No the only issue I see is that a lot of requests will be sent since currency changes quite often, also using pcalls like stated by @ItzMeZeus_IGotHacked
Make sure to use remote events since anything can happen on the client, like the client can give themselves money and if its being handled client side then the server would never know what’s going on.
Little tutorial
Make a remote event in ReplicatedStorage and call it Sell or something better.
-- Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SellEvent = ReplicatedStorage:WaitForChild("Sell") -- I add :WaitForChild just in case
SellEvent.OnServerEvent:Connect(function(player) -- Player is defined as the first parameter always I believe
if player:IsDecendantOf(Players) and player.leaderstats.Money.Value >= ? then -- If needed add more checks
-- Do whatever you need here
end
end
Now the client side is similar and much more simple
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SellEvent = ReplicatedStorage:WaitForChild("Sell")
-- Clicked Event
SellEvent:FireServer() -- This is how you send a event to server
local repstorage = game:GetService("ReplicatedStorage")
local buyevent = repstorage:WaitForChild("BuyEvent")
local item = script.Parent
local shoes = script.Parent.Parent.Parent.Frame1.Shoes
local value = 40000
local players = game:GetService("Players")
local player = players.Player or players.PlayerAdded:wait()
item.MouseButton1Click:Connect(function()
if player:WaitForChild("leaderstats"):WaitForChild("Money").Value >= value then
buyevent:FireServer(value)
item.Text = "purchased"
shoes.Visible = true
value = 0
else
item.Text = "not purchased"
wait(1)
item.Text = "Purchase"
value = 0
shoes.Visible = true
end
end)
– SERVER SCRIPT –
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("MoneySaveSystem")
local repstorage = game:GetService("ReplicatedStorage")
local buyevent = repstorage:WaitForChild("BuyEvent")
local players = game:GetService("Players")
players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder")
folder.Parent = plr
folder.Name = "leaderstats"
local money = Instance.new("IntValue")
money.Parent = folder
money.Name = "Money"
money.Value = 0
money.Value = ds1:GetAsync(plr.UserId)
ds1:SetAsync(plr.UserId, money.Value)
money.Changed:connect(function()
ds1:SetAsync(plr.UserId, money.Value)
end)
end)
buyevent.OnServerEvent:Connect(function(player, value)
player:WaitForChild("leaderstats"):WaitForChild("Money").Value -= value
end)
Make a RemoteEvent instance named “BuyEvent” and place it inside the ReplicatedStorage folder, place the server script inside ServerScriptService folder & place the local script wherever it was placed before inside the ScreenGui.