#Api Problem script doesn't seem to save for some reason...
212 messages · Page 1 of 1 (latest)
script is under server script service
Leaderstats script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Cents = Instance.new("IntValue", leaderstats)
Cents.Name = "Cents"
Cents.Value = 0
local Dollars = Instance.new("IntValue", leaderstats)
Dollars.Name = "Dollars"
Dollars.Value = 0
local CentsInVault = Instance.new("IntValue", leaderstats)
CentsInVault.Name = "CentsInVault"
CentsInVault.Value = 0
local DollarsInVault = Instance.new("IntValue", leaderstats)
DollarsInVault.Name = "DollarsInVault"
CentsInVault.Value = 0
end)
Also under server script service
I'm pretty new to scripting and I wont be online since its getting pretty late but I would appreciate an answer which explains why it doesnt work :))
Full script
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("PlayerMoneyDataStore")
local centsDataStore = DataStoreService:GetDataStore("CentsDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local dollars = leaderstats:FindFirstChild("DollarsInVault")
local cents = leaderstats:FindFirstChild("CentsInVault")
local storedCents
local storedMoney
local success, error = pcall(function()
storedMoney = moneyDataStore:GetAsync(player.UserId)
storedCents = centsDataStore:GetAsync(player.UserId)
end)
if not success then
storedMoney = 0
storedCents = 0
end
cents.Value = storedCents
dollars.Value = storedMoney
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dollars = player.leaderstats and player.leaderstats.DollarsInVault
local cents = player.leaderstats and player.leaderstats.CentsInVault
if dollars and cents then
local success, error = pcall(function()
moneyDataStore:SetAsync(player.UserId, dollars.Value)
centsDataStore:SetAsync(player.UserId, cents.Value)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. error)
end
end
end)
@dull willow did u change the settings of the game to allow saving?
yes
show error message
thats the problem, there is no error message
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("PlayerMoneyDataStore")
local centsDataStore = DataStoreService:GetDataStore("CentsDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local dollars = leaderstats:FindFirstChild("DollarsInVault")
local cents = leaderstats:FindFirstChild("CentsInVault")
local storedCents
local storedMoney
local success, error = pcall(function()
storedMoney = moneyDataStore:GetAsync(player.UserId)
storedCents = centsDataStore:GetAsync(player.UserId)
end)
if not success then
storedMoney = 0
storedCents = 0
end
cents.Value = storedCents
dollars.Value = storedMoney
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dollars = player.leaderstats and player.leaderstats.DollarsInVault
local cents = player.leaderstats and player.leaderstats.CentsInVault
if dollars and cents then
local success, error = pcall(function()
moneyDataStore:SetAsync(player.UserId, dollars.Value)
centsDataStore:SetAsync(player.UserId, cents.Value)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. error)
end
end
end)
so like when u rejoin u don't have ur data?
yep
and ur testing this alone right?
yea
I tested it inside the game aswell
like outside of roblox studio
for some reason it doesnt seem to work
yeah doesn't matter where u test it just wanted to know if it was overloaded
why u using 2 seperate data stores?
I can use 1?
yeah u can save data in tables
like I said I'm pretty new to scripting
saving values seperately would only be useful in ordered data stores
and I know I can probably make a value inside the player instead of leaderstats but idk if that will change anything
I mean would that work
** You are now Level 8! **
this seems wrong
local dollars = player.leaderstats and player.leaderstats.DollarsInVault
local cents = player.leaderstats and player.leaderstats.CentsInVault
why "and" and not just +
or are you trying to save them seperately?
script is server sided right?
yea
alr good
issue doesn't seem very obvious so far..
making these waitforchild is better
local dollars = leaderstats:FindFirstChild("DollarsInVault")
local cents = leaderstats:FindFirstChild("CentsInVault")
Are you sure you're saving what you think youre saving? I'd confirm with some prints just before you save
yea I'm pretty sure
its a .value so its good
here is an issue
are you sure you want to have "player.leaderstats"?
not like player.leaderstats.dollars.value?
yea
might need a value behind this
if I just delete the and
and then just do player:findfirstchild("leaderstats") blah blah
woah
would that work?
what is it
actually
try the script but without the "player.leaderstats and"
cuz im not really sure what thats supposed to do here
I asked chat gpt to fix it and he did that so idk either
thats why I asked
alr I'm trying that now
chatgpt doesn't know a thing thats the first thing u learn from using chatgpt
yea but when I need to do something I don't know about and I want it explained I generally go for chatgpt
documentation explains stuff more clearly, chatgpt can be used to generate simple examples
change these to waitforchild because findforchild skips if there is none
idk what happened but my roblox studio is freaking out rn
Id say just confirm what youre saving via print statements before you save. Even if you think you know l
wait for child generally gives me an error so I tend not to use it anymore
it say's infinite yield on something
thats why I use if statements under most of my variables
this part is most likely the issue because in the saving hes doing dollars.value and the player.leaderstats has no value and with getting the data hes only getting 1 value not a table
waitforchild gives an error "infinite yield" if the child doesn't exist for a long time, waitforchild is good to wait before stuff is loaded tho
and in ur situation that is good, because you're already ensuring it exists just waiting for it to load
try the script without the player.leaderstats and
yea I'm trying to try it lol
but I cant move my mouse in game for some reason rn
I have to restart my computer or something
wait I'll let you know I'm gonna restart the pc
alr
Well if I do this:
local thing = Instance.new("Folder")
local otherThing = Instance.new("IntValue")
print(thing and otherThing.Value)
I do get 0, so I don't think that'd be a problem unless leaderstats is nil
and if I do without the .Value, I do get the value itself
everything else looks fine so we'll wait to see what deleting it does
also hes doing .value on "thing" aswell if u use that situation
What I'm saying is that, as long as there are leaderstats, using the and gets it fine
I do think not waiting for it may absolutely be leading to both dollars and cents being nil, which I think you mentioned in your messages about WaitForChild
waitforchild was just to make the script safer, i didn't say it was the issue
Oh, well I'm saying it certainly could be. He doesn't confirm whether the save actually happened, just if it failed, so I think the save isn't happening at all.
my studio isn't working properly so I can't test it rn
I have to re download it or something Ig
wait
yes?
waitforchild may be the issue because you're using findfirstchild and you're not 100% sure it loaded yet because the script already fires when the player joins
so its just a race between the leaderstats setter and the saver
alr so I use waitforchild
after local leaderstats do print(leaderstats)
and I'll change the and thing that chatgpt made
yeah i don't really know what chatgpt was doing but it doesn't seem right
yea
chatgpt outdated anyways if you're using the free version, copilot better
oh good to know
you can ask an ai when their knowledge was last updated to know if its useful for your situation or not
@dull willow stop
just got info that roblox studio is down
mine ain't work either
can you still open studio?
ye
only the camera is down, scripting is fine
btw how do you post something in a script format in discord lol
you added waitforchild and removed the leaderstats thingy?
it's a script where I have to use a vault gui to know if I can deposit or not but when I modify the leaderstats from directly the script it stays at 0 so it probably works
wait
@dull willow make the script save a value you want to test to 4
there is something wrong
and then revert it back after its 4
final script is this
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("PlayerMoneyDataStore")
local centsDataStore = DataStoreService:GetDataStore("CentsDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local dollars = leaderstats:FindFirstChild("DollarsInVault")
local cents = leaderstats:FindFirstChild("CentsInVault")
local storedCents
local storedMoney
local success, error = pcall(function()
storedMoney = moneyDataStore:GetAsync(player.UserId)
storedCents = centsDataStore:GetAsync(player.UserId)
end)
if not success then
storedMoney = 0
storedCents = 0
end
cents.Value = storedCents
dollars.Value = storedMoney
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local dollars = leaderstats:WaitForChild("DollarsInVault")
local cents = leaderstats:WaitForChild("CentsInVault")
if dollars and cents then
local success, error = pcall(function()
moneyDataStore:SetAsync(player.UserId, dollars.Value)
centsDataStore:SetAsync(player.UserId, cents.Value)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. error)
end
end
end)
?
I forgot to add an end Ig or maybe it was just on discord idk
prob ur language not detecting the word as an actual word causing red lines
This may not always work on the first player that joins.
It's possible the player joins before you connect to PlayerAdded
wdym???
@dull willow try doing this to check if the data loads correctly
just set it to 4 for debugging
alr what if I use
local players = game:getservice("players")
if players then
When this script runs, it's possible that the player is already in the game, so PlayerAdded wont fire since the player is already there. Happened to me before.
uh what should happen
the value should be 4 on leaderstats right?
never happened to me
when you leave it sets it to 4
and when you join it should change the value to 4 if it works
just a quick way to double check
pretty sure it should work
I'd take out the function you connect to PlayerAdded so its on its own (like function initPlayer(player) or whatever), connect that to PlayerAdded, and then run through all players currently in the game and do that function on each of them
hmm
I've had it happen to me before, I was joining too fast before the script could run.
if ur paranoid about that stuff u can just change it to this
What would that do
get the player service faster
this prints works btw
local success, error = pcall(function()
storedMoney = moneyDataStore:GetAsync(player.UserId)
storedCents = centsDataStore:GetAsync(player.UserId)
print("works")
end)
and still no errors
sorry i haven't really looked at this post but have you tried putting a print statement to see if it runs
print storedMoney and storedCents
I don't think that addresses the concern though
yes I'll do that
then what do you suggest to load player data?
playedadded is the best way
0 0
I never said not to use it, I just said to make sure to init all players currently in the game as well just in case PlayerAdded is connected to after someone joins
number
in the million of people who have joined my game that has literally never happened so i doubt its an issue
alr thats good, so the loading of data is safe
then the issue is in the saving or setting of the data
wait. where did u add the print?
local Players = game:GetService("Players")
local function initPlayer(player)
-- do stuff
end
Players.PlayerAdded:Connect(initPlayer)
for _, player in ipairs(Players:GetPlayers()) do
initPlayer(player)
end
Is all I mean. It has happened to me before so I was suggesting caution just in case it may happen.
before or after it checks if its a success
why is it so silent i thought it was just silent cuz my wifi cut out
did i crash?
under here
local success, error = pcall(function()
storedMoney = moneyDataStore:GetAsync(player.UserId)
storedCents = centsDataStore:GetAsync(player.UserId)
end)
player added function
so you didn't put it under
no
e
I put a warn there just to be sure
did warn(error)
nothing happened
so def no error
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Cents = Instance.new("IntValue", leaderstats)
Cents.Name = "Cents"
Cents.Value = 0
local Dollars = Instance.new("IntValue", leaderstats)
Dollars.Name = "Dollars"
Dollars.Value = 0
local CentsInVault = Instance.new("IntValue", leaderstats)
CentsInVault.Name = "CentsInVault"
CentsInVault.Value = 0
local DollarsInVault = Instance.new("IntValue", leaderstats)
DollarsInVault.Name = "DollarsInVault"
CentsInVault.Value = 0
end)```
bro
it say's centsinvault.value
under dollarsinvault
but I doubt thats the issue
It defaults to 0 so I think you're fine
i see the issue
you're setting the value to 0
i had this issue too, the setting to 0 is slower than the script and its automatically 0
you can add a verification that its fully loaded or combine the scripts
yeah combining the scripts here looks easier
will also remove the need for the waitforchild
alr
tomorrow try making the values in the saving script BEFORE you load the data
so you're 100% sure its correct
if I have 2 seperate player added functions in one script would that break it
yeah tought so