My shop isnt working/saving

Hello. My shop isnt saving data at all and I really need help. It works how I want it to, it just dose not save anyhting when you rejoin. If someone could please help me I would be so thankful

This is my script in the button you click

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)

This is my data Store script

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 please

1 Like
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

	local Money = Instance.new('NumberValue', leaderstats)
	Money.Name = "Money"
	Money.Value = 0

	local value1Data = Money

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
	end)

	if s then
		Money.Value = value1Data --setting data if its success
	else
		game:GetService("TestService"):Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Money.Value) --setting data
	end)
	if not s then game:GetService("TestService"):Error(e) 
	end
end)

Try this script for the DataStore.

1 Like

The datastore requests are likely failing since I noticed you aren’t using pcall. Additionally, :UpdateAsync is the preferred method for setting a value that already exists instead of :SetAsync, which should only be used when creating a value for a new player.

this didnt work, It dose save how much money I had before I bought something though

Ah, then that means there’s something wrong with your shop button script. Give me one moment.

Woulndt you need to use remote events? Idk

Or just the datastore itself ?

Make a RemoteEvent inside ReplicatedStorage and name it ShoeEvent
Put the shoes in ServerStorage.

Then replace the button script you have above with this:

local item = script.Parent
local player = game.Players.Localplayer
item.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ShoeEvent:FireServer()
       if player.leaderstats.Money.Value >= 40000 then
          item.Text = "Purchased"
       else
          item.Text = "Not Enough Money"  
          wait(1)
          item.Text = "Purchase"
       end
end)

Create a script in ServerScriptService and name it whatever you want.
Paste this into the script you just created:

game.ReplicatedStorage.ShoeEvent.OnServerEvent:Connect(function(player)
		if player.leaderstats.Money.Value >= 40000 then -- Price of Shoes
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - 40000 -- Subtracts Money from Player
		game.ServerStorage.Shoes:Clone().Parent = player.Backpack -- Clones Shoes from ServerStorage and gives it to the player who bought it
		end	
end)

The thing is “Shoes” isnt a tool its just a button. I can code what the “Shoes” button dose later if that makes sense

So nothing would go in backapack or be cloned

Like I dont need anything to do with a tool just the button, I can put the script in later

Avoid making multiple threads about the same topic.

Yeah dude u alr made tons of topics same issue just stop it

Sorry. Im very new to this and i will just delte them

edit- it wont let me

can u just select the whole screengui and also select the server script, download as a file and send it cuz idk your code’s position