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

I am having trouble with datastore. anything to say? I don't know what to do i tried everything

I watched tons of datastore videos and see NOTHING wrong with my code

local DSService = game:GetService("DataStoreService"):GetDataStore("Currency")

game:GetService("Players").PlayerAdded:connect(function(plr)
	local leader = Instance.new("Folder",plr)
	leader.Name = "leaderstats"
	
	local Credits = Instance.new("IntValue",leader)
	Credits.Name = "Credits"
	
	local Diamonds = Instance.new("IntValue",leader)
	Diamonds.Name = "Diamonds"
	
	local key = "player-"..plr.UserId
	
    local savedData = DSService:GetAsync(key)
 	
	if savedData then
      Credits.Value = savedData[1]
 	  Diamonds.Value = savedData[2]
	else
	  local values = {Credits.Value, Diamonds.Value}
	  DSService:SetAsync(key,values)
	end
end)

game:GetService("Players").PlayerRemoving:connect(function(plr)
	local ValuesToSave = {plr.leaderstats.Credits.Value, plr.leaderstats.Diamonds.Value}
	DSService:SetAsync(plr.UserId, ValuesToSave)
end)

while wait(300) do
	for i,v in pairs(game:GetService("Players"):GetChildren()) do
		local key = v.UserId
		DSService:SetAsync(key,v.leaderstats.Credits.Value,v.leaderstats.Diamonds.Value)
	end
end

1 comment

0

Aren't you supposed to parent those to the player?

1 answer

✓ Accepted answer

First, the GetAsync part is really messed up. The arguments are completely messed up. The correct arguments are:

GetAsync(plr.UserId .. "-NameOfTheScopeHere")

Also, do not use variable to replace the plr.UserId, it'll mess it up. You should use:

local savedData = DSService:GetAsync(plr.UserId .. "-WhateverNameYouWant")

Second, the SetAsync are also really messed up. Once again, the arguments are wrong. The correct arguments should be:

SetAsync(plr.UserId.. "-TheNameShouldBeSameWithTheUpperOneOrDataWillNotLoad", ValueToSave)

In that way your data saving problem should be gone forever.

A little tip: Consider using UpdateAsync, because if you use too much SetAsync, the DataStoreService will overwhelm. SetAsync and UpdateAsync have a different budget. They don't share the same budget. In that way, it's less likely to have data loss reports.

1 comment

0

can you put the complete code on the page so I can see for myself because it still doesn't work

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