This is my buying script

local Purchased2 = script.Parent.Parent.Parent.Frame.Item2.Purchased
local Purchased3 = script.Parent.Parent.Parent.Frame.Item3.Purchased
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Event = RemoteEvents.Buy

	Event.OnClientEvent:Connect(function()
		if cash.Value < price.Value then
			print("NotEnough")
			game.ReplicatedStorage.NotEnough:Clone().Parent = player.PlayerGui
			wait(3)
			player.PlayerGui.NotEnough:Destroy()
		end
			if cash.Value >= price.Value then
			cash.Value = cash.Value - price.Value
			if price.Value == 100 then
				local oldGun = player.Backpack:FindFirstChildOfClass("Tool") or player.Character:FindFirstChildOfClass("Tool")
				oldGun:Destroy()
				game.ReplicatedStorage.Tools.Pistol:Clone().Parent = player.Backpack
				Purchased1.Value = 1
			end
					if price.Value == 300 then
				local oldGun = player.Backpack:FindFirstChildOfClass("Tool") or player.Character:FindFirstChildOfClass("Tool")
				oldGun:Destroy()
				game.ReplicatedStorage.Tools.Revolver:Clone().Parent = player.Backpack
				Purchased2.Value = 1
					end
					if price.Value == 500 then
				local oldGun = player.Backpack:FindFirstChildOfClass("Tool") or player.Character:FindFirstChildOfClass("Tool")
				oldGun:Destroy()
				game.ReplicatedStorage.Tools.Uzi:Clone().Parent = player.Backpack
				Purchased3.Value = 1
			end
		end	
	end)

My Event Script

game.Players.PlayerAdded:connect(function(player)
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Event = RemoteEvents.Buy
local Buy = player.PlayerGui:WaitForChild("Gui").Main.Info.Buy
	Buy.MouseButton1Click:Connect(function()
		Event:FireClient(player)
		print("Worked")
	end)
end)



Saving Script

local DataStore = game:GetService("DataStoreService")
local DS1 = DataStore:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", stats)
	Cash.Name = "Cash"
	
	Cash.Value = DS1:GetAsync(player.UserId) or 0
	DS1:SetAsync(player.UserId, Cash.Value)
	
	Cash.Changed:Connect(function()
		print("SavingData")
		DS1:SetAsync(player.UserId, Cash.Value)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	DS1:SetAsync(player.UserId, player.leaderstats.Cash.Value)
end)

5 comments

0

Where are these located and are they local or server?

0

maybe u need to put the cash value first in a local cash = player.leaderstats.Cash. so when on SetAsync(player.UseId, cash.Value) so cash is checked again as local value and then get saved maybe it will work. let me know

0

You cant access datastores from the client. Thats why I need to know which scripts are server sided and which are client sided

0

I have also wrote more simple and compact data store examples. I suggest you check them out here: https://pastebin.com/2Z3X6USG

0

but be where some local u only need to have once and some u need to stay checking in the functions and other events. like when i use a loop in a script i need to put the local in the loop so the cash is checked over and over again. outside the loop its only checked once and holds only 1 value like with distance checking