Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Data Store isn't working. Can someone please help me fix this problem? I can't find a solution.

(found a solution)

For some reason my Data Store script isn't working. I have checked the API Services and HTTP Requests boxes. Also the data store doesn't work not only in the studio but also in the game itself. There are no errors found aswell Please help!

Here is the script:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Bucks
	local save2 = plr.leaderstats.Character
	local save3 = plr.leaderstats.CA
	
	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
	else
		local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
		ds:GetAsync(plrkey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Bucks.Value, plr.leaderstats.Character.Value, plr.leaderstats.CA.Value})
end)

3 answers

✓ Accepted answer

Hello!

In line 16, do you mean :SetAsync?

ds:GetAsync(plrkey, NumbersForSaving)

Also, I'd recommend for you to specify where the values are located in the table, example:

local foobar = {
	[1] = "foo",
	[2] = "bar"
}

Here and here are developer references about datastores. Check them out sometime, to see if you can find your problem.

If you have any questions, ask them below!

I hope this helps!

12 comments

0

It still doesnt work... One of my values is a string value. Maybe thats why it doesn't work?

0

So, you're telling me you're trying to save something that isnt a stringvalue nor numbervalue?

0

What is it, then?

0

Intvalue and a string value

0

It should save if it isnt a object (for example, a model). I dont know what is wrong.

0

Does it give any errors?

0

no. thats why i don't know what's happening

0

maybe theres something else blocking the data from saving but i cant seem to find anything

0

Have you tried changing player.userId to player.UserId?

0

yes

0

Hmm, I have no idea what could be wrong in your script. Sorry.

0

I checked and only one of the values didn't save. Thank you! Edit: The value didn't save because i did something else wrong.

Change

plr.userId

to

plr.UserId

Also

    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
        save3.Value = GetSaved[3]
    else
        local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
        ds:GetAsync(plrkey, NumbersForSaving)
    end

to

    if GetSaved then
        save1.Value = GetSaved["Bucks"]
        save2.Value = GetSaved["Character"]
        save3.Value = GetSaved["CA"]
    else
        local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
        ds:GetAsync(plrkey, NumbersForSaving)
    end

4 comments

0

it printed this error: ServerScriptService.SaveData:12: invalid argument #3 (string expected, got nil)

0

Don't change the last part only plr.userId to plr.UserId

0

It still doesn't work but now it doesn't print any errors. Maybe theres something wrong with my game?

0

Yeah there's something wrong with your game

just put the plr parameter to player.

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
	    wait()
	    local plrkey = "id_"..player.userId
	    local save1 = player.leaderstats.Bucks
	    local save2 = player.leaderstats.Character
	    local save3 = player.leaderstats.CA
 
	    local GetSaved = ds:GetAsync(plrkey)
	    if GetSaved then
	        save1.Value = GetSaved[1]	        
		save2.Value = GetSaved[2]
	        save3.Value = GetSaved[3]
	    else
	        local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
	        ds:GetAsync(plrkey, NumbersForSaving)
	    end
	end)
game.Players.PlayerRemoving:Connect(function(player)
	    ds:SetAsync("id_"..player.userId, {player.leaderstats.Bucks.Value, 		  		player.leaderstats.Character.Value, player.leaderstats.CA.Value})
end)

4 comments

0

it didn't work

0

oh... I guess you should do it on client? you should change the a code a little bit too.

0

what should i change?

0

I think instead of using a event to get the player you do game.Players.LocalPlayer

This discussion is preserved as it was. New answers, comments, and votes are closed.