Issue with Shop not Saving

I was making candy to cash shop and I’ve run into this issue where it doesn’t save the new cash and candy values. I have a working data store that saves these values when you get them from elsewhere but when it comes down to this candy to cash shop, it doesn’t save. I was maybe thinking it has something to do with the script I made for the shop?

Shop:

local button = script.Parent
local player = game.Players.LocalPlayer
local playerstats = player:WaitForChild("leaderstats")
local CandyStats = playerstats:WaitForChild("Candies")
local CashStats = playerstats:WaitForChild("Cash")
local TextBox = script.Parent.Parent.CandyInput
local Successsound = script.Parent.Parent.Parent.Sounds.Success
local FailSound = script.Parent.Parent.Parent.Sounds.Fail

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
end)

button.MouseButton1Click:Connect(function(plr)
	if (tonumber(TextBox.Text)) > CandyStats.Value then
		FailSound:Play()
		button.BackgroundColor3 = Color3.new(1, 0, 0)
		button.Text = ""
		button.Text = "Insufficient Candies!"
	else
		local inputedamount = tonumber(TextBox.Text)
		Successsound:Play()
		button.BackgroundColor3 = Color3.new(0, 1, 0)
		button.Text = "Sold!"
		TextBox.Text = ""
		CashStats.Value = CashStats.Value + inputedamount
		CandyStats.Value = CandyStats.Value - inputedamount
	end
end)

Data Store:

local DS = game:GetService("DataStoreService")
local SaveData = DS:GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Candies = Instance.new("IntValue")
	Candies.Name = "Candies"
	Candies.Parent = leaderstats
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local plrkey = "id_"..plr.userId
	local CandiesSave = plr.leaderstats.Candies
	local CashSave = plr.leaderstats.Cash

	local GetSaved = SaveData:GetAsync(plrkey)
	if GetSaved then
		CandiesSave.Value = GetSaved[1]
		CashSave.Value = GetSaved[2]
	else
		local NumberForSaving = {CandiesSave.Value, CashSave.Value}
		DS:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	SaveData:SetAsync("id_"..plr.userId, {plr.leaderstats.Candies.Value, plr.leaderstats.Cash.Value})
end)

This is my first Devforum post so if I did something wrong or something weird please tell me

Try using game:BindToClose(). It fires when the server is about to shut down and makes the server wait either 30 seconds or until all tasks have been completed (in this case, data saving).

You can read more about it here: DataModel | Roblox Creator Documentation

1 Like

Your problem is changing the values only for Client and not for Server.
You will need to communicate with server through RemoteEvent/RemoteFunction to change and save your values :grinning_face_with_smiling_eyes:!

For example using RemoteEvent:

Local script ↓

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local RemoteEvent = ReplicatedStorage.RemoteEvent --change this to path to your RemoteEvent
local Button = script.Parent -- change this also to path to your Button that needs to be clicked to send signal to server

Button.MouseButton1Click:Connect(function() 
RemoteEvent:FireServer() 
end) 

Script inside ServerScriptService ↓


local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local RemoteEvent = ReplicatedStorage.RemoteEvent --change this to path to your RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(player) 
local leaderstats = player:FindFirstChild("leaderststs") 
if leaderstats:FindFirstChild("ValueYouWantToCompare1").Value >= leaderstats:FindFirstChild("ValueYouWantToCompare2").Value then
--do stuff
      end
end)
1 Like

Thank you! I ended up using a remote function to do all the GUI stuff but thanks for the help!

Yes I personally use RemoteFunctions too because they can return stuff so it is very handy ;).

1 Like