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

DataStore it's not working on my Leaderstats?

Basically, The Leaderstats work, but the DataStore doesn't when I stop game and run it, the Francs Currency doesn't work

local DataStore = game:GetService("DataStoreService"):GetDataStore("Francs") -- Change the text "Your Data store here" to a name you want"
-- If you don't want the leaderstats to save delete line 1, line 13 through 20


game.Players.PlayerAdded:connect(function(plr)
	local leaderstats = Instance.new("IntValue", plr) -- Creates an Int Values inside of the player
	leaderstats.Name = "leaderstats" -- Names the Int Value
	
	Francs = Instance.new("IntValue", leaderstats) -- Creates another Int Value and parents it to leaderstats
	Francs.Name = "Francs" -- Names the stat (CHANGE the "Your leaderstats here" text to anything you want)


	local key = "plr-"..plr.UserId
	local Saved = DataStore:GetAsync(key)
	if Saved then
		Francs.Value = Saved[1] 
	else
		local ValuesToSave = {Francs.Value}
		DataStore:SetAsync(key, ValuesToSave)
	end
end)
game.Players.PlayerRemoving:connect(function(plr)
	local key = "plr-"..plr.UserId
	local valuesToSave = {Francs.Value}
	
	DataStore:SetAsync(key, valuesToSave)
end)

1 answer

Try this. It's the same thing but uses a pcall. Note that you have to be in-game for DataStores to work. Also, make sure access to API Services are enabled.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Francs") -- Change the text "Your Data store here" to a name you want"
-- If you don't want the leaderstats to save delete line 1, line 13 through 20


game.Players.PlayerAdded:connect(function(plr)
    local leaderstats = Instance.new("IntValue", plr) -- Creates an Int Values inside of the player
    leaderstats.Name = "leaderstats" -- Names the Int Value

    Francs = Instance.new("IntValue", leaderstats) -- Creates another Int Value and parents it to leaderstats
    Francs.Name = "Francs" -- Names the stat (CHANGE the "Your leaderstats here" text to anything you want)


    local key = "plr-"..plr.UserId
    local Saved 

    local success, errorMessage = pcall(function()
    	   saved = DataStore:GetAsync(key)
    end)

   if success then
	    if Saved then
	        Francs.Value = Saved[1] 
	    else
	        local ValuesToSave = {Francs.Value}
	        DataStore:SetAsync(key, ValuesToSave)
	    end
else
	warn(errorMessage)
 end
end)
game.Players.PlayerRemoving:connect(function(plr)
    local key = "plr-"..plr.UserId
    local valuesToSave = {Francs.Value}

    DataStore:SetAsync(key, valuesToSave)
end)

4 comments

0

API is enabled, It's a Group Game. I change the value, but when I like reconnect through studio, it doesn't work, the value changes back to 0

0

DataStores don't work in studio. They only work in the real game.

0

I have had some scripts on the past, they would work on studio too?

0

No.

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