Datastore Not Working

Okay replace value with Value i mispelled it so it isn’t working. Plus you can just because its created in the leaderboard script doesn’t mean it wont work also replace leaderstats with Leaderstats since thats what its called in your script.

1 Like

This works very well.

local DataStore = game:GetService("DataStoreService")
local Players = game:GetService("Players")

function ReadData(Key,GDS)
    local Yeah,LoadGDS = pcall(function()
	    return GDS:GetAsync(Key)
    end)
    return Yeah,LoadGDS
end
function NewPlayer(Player)
	local Stat = Instance.new("Folder",Player)
    Stat.Name = "leaderstats"

	local Clicks = Instance.new("IntValue")
    Clicks.Name = "Clicks"
	Clicks.Parent = Stat

    return Stat
end

function LoadData(Player,Value,Name)
    local Key = Player.userId.."Clicks"
    
    local GDS = DataStore:GetDataStore(Name)
    local Yeah,LoadGDS = ReadData(Key,GDS)

	if Yeah and LoadGDS ~= nil then
    	print("Player Data successfully loaded!")
	    if LoadGDS ~= nil then	Value.Value = LoadGDS
    	else					Value.Value = 0			end
    	print("Last saved Click data:", LoadGDS)
	elseif not Yeah then	warn("Error") end
end

function SaveData(Player,Value,Name)
	local Key = Player.userId.."Clicks"
    
    local GDS = DataStore:GetDataStore(Name)
    local Yeah,LoadGDS = ReadData(Key,GDS)

	if Yeah and LoadGDS == nil then
    	local Save,Err = pcall(function()
    		GDS:SetAsync(Key,Value.Value)
	    end)
	    if Save then	print("Success!")
	    else			warn("Error")	end
	elseif Yeah and LoadGDS ~= nil then 
    	local Save,Err = pcall(function()
	    	GDS:UpdateAsync(Key,function()
		    	return Value.Value
		    end)
		end)
    	if Save then	print("Success!")
    	else			warn("Error")	end
    elseif not Yeah then warn("Error") end
end

function Load(Player)
    local Stat = NewPlayer(Player)

	coroutine.wrap(LoadData)(Player,Stat.Clicks,"ClickDataStore")
end
function Save(Player)
    local Stat = Player.leaderstats

	coroutine.wrap(SaveData)(Player,Stat.Clicks,"ClickDataStore")

    wait(1)
end

Players.PlayerAdded:Connect(function(Player)
	coroutine.wrap(Load)(Player)
    wait(1)
end)

Players.PlayerRemoving:Connect(function(Player)
    coroutine.wrap(Save)(Player)
	wait(1)
end)

game:BindToClose(function()
    for i,Player in pairs(Players:GetPlayers()) do
    	coroutine.wrap(Save)(Player)
    end
    wait(1)
end)

Would you like me to explain how to add new entries or what each one does?

That would be great. Can you explain what each one does?

Also. When you load into the game and leave there is a error.

  10:03:23.245  Error  -  Server  -  ClickDataStore:32
  10:03:40.711  Error  -  Server  -  ClickDataStore:55

It just reads the supplied DataStore and returns if the function worked and the value of the DataStore.

Create a folder and variables with the player’s data.

It will change the data previously given.

Save the data, if it is nil, it is the first time the player joins, if not, it is not the first time the player enters.

It will call the functions to load the data, coroutine.wrap makes it faster.

It will call the functions to save the data, coroutine.wrap makes it faster as if the server shuts down the functions will stop after 60 seconds.

Loads the data when the player joins.

Save the data when the player leaves.

Saves the data when the last player leaves or when you shut down the servers.

It is a Roblox problem, as the script does not cause problems.

Also, you need to enable this in the game settings:

1 Like

That is turned on but its still not working for some reason.