#It wont work, datastore money

1 messages · Page 1 of 1 (latest)

shadow haven
#

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local moneyStore = DataStoreService:GetDataStore("PlayerMoney")

local function setupLeaderstats(player, cashValue)
-- Remove old leaderstats if they exist
local oldStats = player:FindFirstChild("leaderstats")
if oldStats then
oldStats:Destroy()
end

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

local money = Instance.new("IntValue")
money.Name = "Cash"
money.Value = cashValue or 0
money.Parent = leaderstats

local seatvalue = Instance.new("BoolValue")
seatvalue.Name = "SeatValue"
seatvalue.Value = false
seatvalue.Parent = leaderstats

-- Save money when it changes
money.Changed:Connect(function()
    -- Use money.Value instead of the event argument (which is a string)
    pcall(function()
        moneyStore:SetAsync(player.UserId, money.Value)
    end)
end)

end

local function onPlayerAdded(player)
-- Load saved money
local cash = 1000 -- default starting cash
local success, result = pcall(function()
return moneyStore:GetAsync(player.UserId)
end)
if success and result ~= nil then
cash = result
end

setupLeaderstats(player, cash)

end

local function onPlayerRemoving(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local money = leaderstats:FindFirstChild("Cash")
if money then
pcall(function()
moneyStore:SetAsync(player.UserId, money.Value)
end)
end
end
end

#

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

-- Handle players already in the game (in case script is reloaded)
local playerList = Players:GetPlayers()
for i, player in playerList do
onPlayerAdded(player)
end

Anybody an idea why this wont work, I can't find it

grand arrow
#

@shadow haven

grand arrow
#

what does output say?

#

check studio settings

#

this is chatgpt?

#

go to game settings, and then go to settings, and make sure the second option is turned on

ancient pecan
#

yeah u gotta enable api service in game settings if u haven’t

shadow haven
grand arrow
grand arrow
#

stop using chatgpt

#

if anything use deepseek

#

other than that learn yourself

#

deepseek, and claude are way better than skidgpt

shadow haven
shadow haven
obtuse schoonerBOT
#

studio** You are now Level 2! **studio

ancient pecan
#

Great

shadow haven
#

uhm

#

I would check that out

#

I will message here if that works

ancient pecan
#

so u coded everything above?

grand arrow
#

@shadow haven atleast show us the output of the code

shadow haven
grand arrow
#

the player just joined the game

shadow haven
#

true

grand arrow
#

btw show output

shadow haven
#

ok

ancient pecan
#

add debug logs too

#

Also you said it won’t work

#

What isn’t working

#

The saving of the money?

#

Use the pcall to capture the success and error

shadow haven
#

and not at the amount of money with what you left

#

but I think I found it now because in output it says data saves

#

so I will upload it to roblox now

#

and will try it out

#

it works!

#

thanks guys for the help!

ancient pecan
#

So what did u change

shadow haven
#

I added 2 values money and cash

#

first it was only cash

#

and then some stuff that you have sended in that link to the forum

#

also added print data has been saved so I can check it pretty easy in output

#

now I will just add startermoney and maybe something else and then I go to sleep

#

as it's already 1.20 AM for me XD

grand arrow
ancient pecan
grand arrow
#

i need to sleep

#

its 2 am rn

hard storm
#

main thing i see missing is lack of game:bindtoclose. thats why it not saving in studio