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

DataStore says it's saving, but actually not?


local rs = game:GetService("ReplicatedStorage")
local Update = rs:WaitForChild("UpdateGui")
local WinUpdater = rs:WaitForChild("WinsUpdater")
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")


local	 function ClientFire(wins)

		print("super happy")
		Update:FireAllClients(wins)

    
end



	
		
	



local function onPlayerJoin(player)

 
    
    local wins = Instance.new("IntValue")
    wins.Name = "wins"
    wins.Value = 0
    wins.Parent = player



	local ID = player.UserId
	
	local data
	local s, em = pcall(function()
		 return DataStore1:GetAsync(ID, data)
	end)	
	
	if s then
		print("got data!")
		wins.Value = data
	else
			warn(em)
	end


ClientFire(wins.Value)


end   
  






game.Players.PlayerRemoving:Connect(function(player)
	
	local ID = "Player_"..player.UserId
	
	local data = player.wins.Value
	local s, em = pcall(function()
		
	DataStore1:SetAsync(ID,data)
	
	end)

if s then
	print("saved!" .. data)
	
	else
	print("sad")
	warn(em)
end

end)

game.Players.PlayerAdded:Connect(onPlayerJoin)

WinUpdater.Event:Connect(function(tp, plr)
	print("very happy")
local	wins = plr:WaitForChild("wins")
		if tp == 1 then
			wins.Value = wins.Value + 2
	else if tp == 2 then
		wins.Value = wins.Value + 1
	end 
				
		end
		ClientFire(wins)
end)

This is the script. Every time I run it, it says it saves the wins, prints the amount saved, and says it got the data but still never actually saves the wins. Can someone please help me?

1 answer

:GetAsync doesn’t contain a second argument, but simply just the pointer address to the data, which is the UserId. This confuses the method.

local ID = "Player_"..Player.UserId
local Success, Response = pcall(DataStore1.GetAsync, DataStore1, ID)
if (Success) then
    Wins.Value = Response
end

This isn’t the only issue however. The data you allocated contains a separate key, meaning the data you’re trying to fetch won’t exist. The key must match the key used to store the data, in order to receive the same data:

local ID = "Player_".Player.UserId
local Success, Response = pcall(DataStore1.SetAsync, DataStore1, ID, Data)
if (Success) then
    print("Saved Data!")
else
    warn(Response)
end

2 comments

0

Where would I put this code into my script? I'm having some trouble, sorry if it's a stupid question.

0

Here: https://pastebin.com/pgbPuWfP. This should solve all the issues with your code.

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