#Api Problem script doesn't seem to save for some reason...

212 messages · Page 1 of 1 (latest)

dull willow
#

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 :))

dull willow
# dull willow

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)

clever summit
#

@dull willow did u change the settings of the game to allow saving?

dull willow
#

yes

clever summit
#

show error message

dull willow
dull willow
clever summit
#
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?

dull willow
#

yep

clever summit
#

and ur testing this alone right?

dull willow
#

yea

#

I tested it inside the game aswell

#

like outside of roblox studio

#

for some reason it doesnt seem to work

clever summit
#

yeah doesn't matter where u test it just wanted to know if it was overloaded

#

why u using 2 seperate data stores?

dull willow
#

I can use 1?

clever summit
#

yeah u can save data in tables

dull willow
#

like I said I'm pretty new to scripting

clever summit
#

saving values seperately would only be useful in ordered data stores

dull willow
#

and I know I can probably make a value inside the player instead of leaderstats but idk if that will change anything

dull willow
faint graniteBOT
#

studio** You are now Level 8! **studio

dull willow
#

just using 1=

#

?

clever summit
#

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?

dull willow
#

I dont know much about api scripting I'm pretty new to it so

#

idk

clever summit
dull willow
#

yea

clever summit
#

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")
cinder cloak
#

Are you sure you're saving what you think youre saving? I'd confirm with some prints just before you save

dull willow
#

I sent the leaderstats script aswell

#

so you can look at that if you want

clever summit
#

here is an issue

#

are you sure you want to have "player.leaderstats"?

#

not like player.leaderstats.dollars.value?

dull willow
#

yea

clever summit
#

might need a value behind this

dull willow
#

if I just delete the and

#

and then just do player:findfirstchild("leaderstats") blah blah

clever summit
#

woah

dull willow
#

would that work?

clever summit
#

i found an issue

#

pretty big one

dull willow
#

what is it

clever summit
#

actually

#

try the script but without the "player.leaderstats and"

#

cuz im not really sure what thats supposed to do here

dull willow
#

I asked chat gpt to fix it and he did that so idk either

#

thats why I asked

#

alr I'm trying that now

clever summit
dull willow
#

yea but when I need to do something I don't know about and I want it explained I generally go for chatgpt

clever summit
#

change these to waitforchild because findforchild skips if there is none

dull willow
#

idk what happened but my roblox studio is freaking out rn

cinder cloak
#

Id say just confirm what youre saving via print statements before you save. Even if you think you know l

dull willow
#

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

clever summit
clever summit
#

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

dull willow
#

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

clever summit
#

alr

cinder cloak
#

and if I do without the .Value, I do get the value itself

clever summit
clever summit
cinder cloak
clever summit
cinder cloak
dull willow
#

my studio isn't working properly so I can't test it rn

#

I have to re download it or something Ig

clever summit
#

wait

dull willow
#

yes?

clever summit
#

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

dull willow
#

alr so I use waitforchild

clever summit
#

after local leaderstats do print(leaderstats)

dull willow
#

and I'll change the and thing that chatgpt made

clever summit
dull willow
#

yea

clever summit
#

chatgpt outdated anyways if you're using the free version, copilot better

dull willow
#

oh good to know

clever summit
#

you can ask an ai when their knowledge was last updated to know if its useful for your situation or not

dull willow
#

wait

#

studio opened I just need to figure out how to do the mouse thing

clever summit
#

@dull willow stop

#

just got info that roblox studio is down

#

mine ain't work either

dull willow
#

ohhh

#

alr

clever summit
#

can you still open studio?

dull willow
#

ye

clever summit
#

only the camera is down, scripting is fine

dull willow
#

btw how do you post something in a script format in discord lol

clever summit
dull willow
#

been trying to figure that out for a while

#
print("Hi")
#

oh

#

ty

#

I think it works

clever summit
#

you added waitforchild and removed the leaderstats thingy?

dull willow
#

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

clever summit
#

alr wait theres a way to test this

#

dang it i closed studio wait

dull willow
#

wait

clever summit
#

@dull willow make the script save a value you want to test to 4

dull willow
#

there is something wrong

clever summit
#

and then revert it back after its 4

dull willow
#

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)
clever summit
#

?

dull willow
#

I forgot to add an end Ig or maybe it was just on discord idk

clever summit
#

prob ur language not detecting the word as an actual word causing red lines

cinder cloak
clever summit
#

@dull willow try doing this to check if the data loads correctly

#

just set it to 4 for debugging

dull willow
#

alr what if I use
local players = game:getservice("players")
if players then

cinder cloak
# clever summit wdym???

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.

dull willow
#

the value should be 4 on leaderstats right?

clever summit
#

and when you join it should change the value to 4 if it works

#

just a quick way to double check

dull willow
#

damn

cinder cloak
clever summit
#

hmm

cinder cloak
clever summit
cinder cloak
clever summit
dull willow
#

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

remote bear
#

sorry i haven't really looked at this post but have you tried putting a print statement to see if it runs

clever summit
cinder cloak
dull willow
#

yes I'll do that

clever summit
#

playedadded is the best way

dull willow
#

0 0

clever summit
#

not needed for cents

cinder cloak
dull willow
#

number

clever summit
clever summit
#

then the issue is in the saving or setting of the data

#

wait. where did u add the print?

cinder cloak
clever summit
#

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?

dull willow
#

player added function

clever summit
#

so you didn't put it under

dull willow
#

no

clever summit
#

right?

#

ok then the loading of data is fine

dull willow
#

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

cinder cloak
#

It defaults to 0 so I think you're fine

clever summit
#

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

dull willow
#

so what do I do

#

just set no value

clever summit
#

you can add a verification that its fully loaded or combine the scripts

#

yeah combining the scripts here looks easier

dull willow
#

hm

#

alr

clever summit
#

will also remove the need for the waitforchild

dull willow
#

how

#

bro I'm to tired to work on this rn

#

I'll keep working on it tomorrow

clever summit
#

alr

dull willow
#

thanks for all your help

#

205 messages lol

clever summit
#

tomorrow try making the values in the saving script BEFORE you load the data

#

so you're 100% sure its correct

dull willow
#

if I have 2 seperate player added functions in one script would that break it

clever summit
#

you don't have to have 2 seperates

#

you can fit them in 1

dull willow
#

yeah tought so