Uhhhh my cash counter wont subtract

my money wont subtract when i click a button

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.ReplicatedStorage.Money.OnServerEvent:Connect(function(plr)
	if plr.leaderstats.Money.Value >= 100 then
		plr.leaderstats.Money.Value -= 100
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local Money = Instance.new("NumberValue")
	Money.Name= "Money"
	Money.Parent = leaderstats
	
	local playerUserId = "Player_"..plr.UserId
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		Money.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local playerUserId = "Player_"..plr.UserId
	
	local data = plr.leaderstats.Money.Value
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("success")
	else
		print("not successful")
		warn(errormessage)
	end
end)

heres another script

crate1.MouseButton1Click:Connect(function()
	-- Get a reference to the player
	local plr = game.Players.LocalPlayer

	-- Check if the player has enough money (assuming the required amount is 100)
	if plr and plr.leaderstats and plr.leaderstats.Money and plr.leaderstats.Money.Value >= 100 then
		opening.Visible = true
		shopmain.Visible = false
		shopx.Visible = false
		crate2.Visible = false
		crate1.Visible = false

		wait(1)
		opening.Visible = false

		local get = math.random(1, 5)
		print(get)

		player:SetAttribute(key, get)
	else
		-- Print a message or take some other action to indicate insufficient funds
		print("Insufficient funds to open the crate.")
	end
end)
1 Like

You aren’t even telling the server to subtract the money. Do game.ReplicatedStorage.Money:FireServer() right under the if statement

Hey, you cannot really subract the value properly by Local Script. It also needs to be done on Server. Best to use RemoteEvents. They can fire and send values from Local to Server. You should learn what these are.

RemoteEvent | Documentation - Roblox Creator Hub.

1 Like

forgot to show this

local db = true

script.Parent.MouseButton1Click:Connect(function()
if db == true then
db = false

	-- Get a reference to the player
	local plr = game.Players.LocalPlayer

	if plr and plr.leaderstats and plr.leaderstats.Money and plr.leaderstats.Money.Value >= 100 then
		game.ReplicatedStorage.Money:FireServer()
	else
		print("not enough cash")
	end

	wait(1)
	db = true
end

end)

it was working earlier until I added the save data part

1 Like

the if check might be disturbing it, add a print inside the if to see if it works

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.