Datastore not working

Hello, ive been attempting to create a new game but! One massive issue I’m having is datastore does not work.

I have tried 2 seperate datastores that I have made and none work,

Is there an issue with the game? API services is enabled

Or is this just possibly an issue with roblox servers?

Going to have to be more specific. Maybe post some code that you think should work.

If Data were broken site-wide, developers would be howling bloody murder here, so I doubt that’s the case.

6 Likes

Are there any errors in the output?

Sorry, there is no errors in the output. This is the simple one as i’m not confident in posting this on the devforum unless it was via a private message

local Data = game:GetService("DataStoreService"):GetDataStore("TEGAME1")

game.Players.PlayerAdded:Connect(function(plr)
    local s = Instance.new("Folder",plr)
    s.Name = "Stats"

	local coins = Instance.new("NumberValue",s)
	coins.Name = "Coins"
	coins.Value = 0
	
	local rebirths = Instance.new("NumberValue",s)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	
    local Data = Data:GetAsync(tostring(plr.UserId))
    
    if Data then
        coins.Value = Data[1] or 0
		rebirths.Value = Data[2] or 0
		
    end  
    end)


game.Players.PlayerRemoving:Connect(function(plr)
    local stats = plr.Stats
    Data:SetAsync(tostring(plr.UserId), {

        stats.Coins.Value,
		stats.Rebirths.Value,
       
    })
end)

There should be more values, there is only a limited amount as ive been trying to get it to work, i’ve been trying all day near enough.

use

local success, userid = pcall(function()
    return Data:GetAsync(tostring(plr.UserId))
end)
and
local success, err = pcall(function()
   Data:SetAsync(tostring(plr.UserId), {

        stats.Coins.Value,
		stats.Rebirths.Value,
       })
end)

And also, Data is already defined at the top

Line 21: 1 is not a valid member of global datastore.

local Data = game:GetService("DataStoreService"):GetDataStore("TEGAME1")

game.Players.PlayerAdded:Connect(function(plr)
    local s = Instance.new("Folder",plr)
    s.Name = "Stats"

	local coins = Instance.new("NumberValue",s)
	coins.Name = "Coins"
	coins.Value = 0
	
	local rebirths = Instance.new("NumberValue",s)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	
    local success, userid = pcall(function()
    	return Data:GetAsync(tostring(plr.UserId))
	end)
    
    if userid then
        coins.Value = Data[1] or 0
		rebirths.Value = Data[2] or 0
		
    end  
    end)


game.Players.PlayerRemoving:Connect(function(plr)
    local stats = plr.Stats
	local success, err = pcall(function()
	   Data:SetAsync(tostring(plr.UserId), {
	
	        stats.Coins.Value,
			stats.Rebirths.Value,
	       })
	end)
end)

Updated code, error should be fixed now

I still get the same error ‘1’ is not a valid member of globaldatastore

Hmmm, interesting, I don’t get an error

Could another script interrupt this?

I have some screen guis checking the coin value and a rebirth menu tab that takes the values of them to figure out the cost.

You should do. You are trying to index Data which is still just the datastore.

So what is the fix, Remove it?

Swap Data[1] and Data[2] for userid[1] and userid[2] in that person’s answer, though I disagree with the naming of that variable as it’s a table of player data, not a user id.

Okay thank you,

*That doesn’t work neither.

What i’m mostly confused about though is this has worked in other games of mine but why not this one?

I assume you’re testing in-game and not in studio right? I see no issue with the code if you’ve made those changes.

Are you changing the values on the client or the server before saving? If on the client then obviously the server version would still be 0 and would save 0 to your datastore.

If in studio I have had a couple of occasions where the game is closed before it saves. Try adding a game:BindToClose( callback ) where callback is a function that either arbitrarily holds it open by doing wait( 30 ) for example, or more appropriately perform a save of all players’ data (loop through game.Players:GetPlayers() and save everyone’s stats).

I’ve been testing in studio but the odd time when i would test in game it would not save. Hang on ill try it now and see what happens.

Oh my god, thank you

The changes weren’t happening on the server just the client :man_facepalming:

It’s a clickable GUI that gives you cash though, and it could be very easily hacked through remotevents, Is there any way i could secure the game?

Also ive never had an issue with adding stats on the client before?

If you haven’t had an issue before then those games are probably not secure and don’t use Filtering.

In terms of the remote events, any checks you do on the client should be done on the server too, for example a time-based check if it’s free money upon clicking some button regularly. So on the server you’d store when the player last claimed the cash, and check that sufficient time has passed, something along the lines of tick() - lastCashTick >= CASH_TIMEOUT where CASH_TIMEOUT is a variable set to the minimum number of seconds to enforce between claims.

They use filtering enabled as they were made around a month ago.

Is it maybe because I’m using a different way?

Also okay so I should just do checks on the server not the client??