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)