#Data Save Not Loading Back In

110 messages · Page 1 of 1 (latest)

abstract igloo
#

Whenever I made this code and ran it, it says "connect from ::ffff:127.0.0.1|65428 - Studio
15:06:13.450 ▶ DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 328975515"

And whenever I reload back in the game the DataStore doesnt load in. Please help. Code is provided below:
``local stat = "Cash"

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = stat
Cash.Value = ds:GetAsync(player.UserId)
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
print("it changed")
ds:SetAsync(player.UserId, Cash.Value)

end)
end)

game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)
end)
``

vivid bramble
# abstract igloo Whenever I made this code and ran it, it says "connect from ::ffff:127.0.0.1|654...

It looks like your code is correctly using Roblox's DataStoreService to store player data. However, it's possible that there may be some issues with how the data is being loaded and saved.

One potential issue is that the "Cash" value is being set to the result of ds:GetAsync(player.UserId), but this may return nil if there is no previous data stored for the player. In this case, it might be a good idea to check if the result is nil and set the Cash value to a default value (e.g. 0) in that case.

Another potential issue is that the data is being saved to the datastore in the Cash.Changed event handler. This event is fired whenever the Cash value changes, which means that the datastore will be updated frequently and may cause unnecessary delays and possibly rate limiting issues with the datastore. It might be better to save the data only when the player is leaving the game (in the PlayerRemoving event handler), or perhaps use a debounce function to limit the frequency of saves.

Here is an updated version of your code that addresses these issues:

#

Note that this code stores the Cash value in a player attribute to make it easier to access and update, and only saves the data to the datastore when the player is leaving the game. The Cash value is also set to a default value of 0 if it is nil.

abstract igloo
#

u typed that so fast.

#

ty ill check it out

#

@vivid bramble it says "Instance is not a supported attribute type " in your new code meaning that i cant store it in attributes?

vivid bramble
#

This code should work without errors and correctly store the player's cash value in the datastore.

light vigil
#

Convert value to number

abstract igloo
#

damn

#

ur goated

#

kk ill use this code and what u said dark

#

I am kinda confused bc it keeps setting the games cash to 100

#

which is weird because the code never states anythign related to that

light vigil
#

print cashvalue when you get it from datastore

abstract igloo
#

kk

#

its output is 100

errant merlinBOT
#

studio** You are now Level 6! **studio

abstract igloo
#

i think i know the reason

#

my previous code saved it like 100 times

#

is it loading it in now?

#

for the next 100 saves?

light vigil
#

Does it print this ?

abstract igloo
#

yep

#

im trying to add 1 to the value

#

stat.Value = stat.Value +1

light vigil
#

It print 1 ?

abstract igloo
#

idk why it isnt working

#

its such a simple line of code yet i cant right it

#

yet i have been writing a ds script

light vigil
abstract igloo
#

100

abstract igloo
light vigil
#

O

abstract igloo
#

whats that mean?

vivid bramble
#

It's possible that there is another part of your code that is setting the cash value to 100, or there could be a default value set somewhere in the game's settings.

You can try adding some additional print statements to the code to see if the cash value is being set to 100 in the ds:GetAsync(player.UserId) call. For example, you can add the following lines of code to the PlayerAdded event handler:

#

This should print out the actual value retrieved from the datastore, which can help you determine where the 100 value is coming from.

If you can't find the source of the 100 value, you can try setting a different default value for the cash value, such as -1, and see if that helps. If the GetAsync call still returns 100, then there may be an issue with the datastore itself.

light vigil
abstract igloo
#

whats that-

#

ohhhhhh

#

shoot

#

that doesnt mean anyrthing tho?>

light vigil
#

stat.Value can't work since stat is a string

vivid bramble
#

Do you know the source of the 100 value?

abstract igloo
#

im trying to implement ur code in rn

#

its not telling me the source

#

its weird

#

ima make another game and copy paste the code into that

#

see if that works

#

yep it was a prob with my prev game

#

its set to nil here now

vivid bramble
#

Glad to have helped :D

abstract igloo
#

"ServerScriptService.Script:38: attempt to perform arithmetic (add) on nil and number "

abstract igloo
#

the last add code i added

#

i cant add a value to the thing

#

which is confusing

abstract igloo
#

i added the cash val to it

#

it still saved it with 0

#

im legit stupid or smhtn

#

im missing smhtn

vivid bramble
#

If the cash value is still not being saved to the datastore, there could be a few reasons why:

The ds:SetAsync method is not being called. Check if the PlayerRemoving event is firing correctly by adding a print statement to it:game.Players.PlayerRemoving:Connect(function(player)
local cashValue = player:GetAttribute(stat)
if cashValue then
ds:SetAsync(player.UserId, cashValue)
print("Cash saved for player " .. player.Name .. " with value " .. cashValue)
else
print("Cash value is nil for player " .. player.Name)
end
end)
If you don't see the "Cash saved" print statement in the output when a player leaves the game, then the PlayerRemoving event may not be firing.

#

2

#

The player:GetAttribute(stat) method is returning nil. Check that the cash value is being correctly stored in the player's attribute by adding a print statement to the PlayerAdded event:game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = stat
local cashValue = ds:GetAsync(player.UserId)
Cash.Value = cashValue or 0 -- Set to default value if data is nil
player:SetAttribute(stat, Cash.Value) -- Store value as number in attribute for easier access
print("Cash value for player " .. player.Name .. " set to " .. tostring(Cash.Value))

Cash.Changed:Connect(function()
    player:SetAttribute(stat, Cash.Value) -- Update value in attribute when it changes
    print("Cash changed for player " .. player.Name .. " to " .. Cash.Value)
end)

end)
If you don't see the "Cash value" print statement in the output when a player joins the game, then the player:SetAttribute(stat, Cash.Value) method may not be storing the cash value in the player's attribute correctly.

#

If you still can't find the issue, try using the pcall function to catch any errors that may be occurring when the datastore is being saved or loaded. Here's an example:

#

game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new("Folder", player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue", leader)
Cash.Name = stat

local success, cashValue = pcall(function()
    return ds:GetAsync(player.UserId)
end)

if success and cashValue ~= nil then
    Cash.Value = cashValue
    player:SetAttribute(stat, Cash.Value)
    print("Cash value for player " .. player.Name .. " set to " .. tostring(Cash.Value))
else
    print("Error retrieving cash value for player " .. player.Name)
    Cash.Value = 0
end

Cash.Changed:Connect(function()
    player:SetAttribute(stat, Cash.Value)
    print("Cash changed for player " .. player.Name .. " to " .. Cash.Value)

    local success, result = pcall(function()
        ds:SetAsync(player.UserId, Cash.Value)
    end)
    if not success then
        print("Error saving cash value for player " .. player.Name)
    end
end)

end)

game.Players.PlayerRemoving:Connect(function(player)
local cashValue = player:GetAttribute(stat)
if cashValue ~= nil then
local success, result = pcall(function()
ds:SetAsync(player.UserId, cashValue)
end)
if success

abstract igloo
#

i am overwhelmed

#

my feeble brain

light vigil
#

Cause datastore calls are limited

abstract igloo
#

oh

#

ima delete that part

abstract igloo
light vigil
#

Have you add 1 ?

abstract igloo
#

yes

light vigil
#

how?

abstract igloo
#

added it inside of the

#

player added

#

function

#

im gonna kill myself bro

light vigil
#

Cash.Value = Cash.Value +1?

abstract igloo
#

Cash.Value += 1

#

why does that mattert

light vigil
#

No

abstract igloo
#

sorry im getting a bit mad

#

ima chill a bit

#

ok im fine

light vigil
#

Printed ?

abstract igloo
#

but when it was there

#

it never ran.

#

ima add it back

abstract igloo
#

on the last line

light vigil
#

try
player:SetAttribute(stat,player:GetAttribute+1)

abstract igloo
#

nope

#

i edited it a bit

light vigil
#

print cashValue before saving to datastore

abstract igloo
#

it alreaady it

#

does

light vigil
#

It print 0?

abstract igloo
#

prints nil

#

which is 0

#

so ye

#

when i add one it would print 1

#

well.

#

I may just give up