How can I add Data Store to this script?

Hello, I’m looking for a way to add datastore into this script so that the Jumps stats in the leaderboard remain updated when the player joins a new server but I have no clue on how to do it, any help?

The script is in ServerScriptsService and I named it Jumps if that can help.

Script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player 

	local jumpCount = Instance.new("IntValue")
	jumpCount.Name = "Jumps"
	jumpCount.Parent = leaderstats

	player.CharacterAdded:Connect(function(character) 

		local humanoid = character:WaitForChild("Humanoid")
		local debounce = true

		humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
			if debounce == true then
				debounce = false
				if humanoid.Jump == true then
					jumpCount.Value = jumpCount.Value + 1
				end
				wait(0.2)
				debounce = true 
			end
		end)
	end)
end)

Thanks in advance to everyone! :grinning:

2 Likes

datastore

1 Like

Firstly, get your jumps datastore

local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")

After that, make a function (i prefer using functions as it makes script readable)

local function loadData(datastore, key)
   local success, data = pcall(function()
       return datastore:GetAsync(key)
   end

   return data
end

Imagine datastore as a dictionary/object.

local dataDictionary = {
   ['12345'] = 125, -- [userid] = value,; we'll use this format.
}

Now that we have load, we need a save function.

local function saveData(datastore, key, value)
   local success, err = pcall(function() -- "pcall stands for protected call"
	  datastore:SetAsync(key, value)
   end)

   if err then print(err) end
end

After all the functions were initialized, just simply use them!

--blablabla
game.Players.PlayerRemoving:Connect(function(player)
      local jumps = player.leaderstats.Jumps

      saveData(jumpsDatastore, player.UserId, jumps.Value) -- It's not recommended to use the name as the key, a user's name can be changed, but user id doesn't
   end
end
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0 -- if data exists then set the jumpcount value according to the data, else set it to 0

I recommended you to read this article for further information.

One problem here is that your using your saveData function everytime a player jump’s which is kind of unnecessary work for the server. Save your data every 5 minutes or when the player leaves instead

Oh right, i was about to edit the post as soon i realized that.

How would I add them to the script? Just in order or do I need to get them in select positions?

For example

local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")

-- and then continue the script in order

Sorry about the many questions but I’m not a very experienced scripter

You can put the save and load functions anywhere except below playerAdded or playerRemoving (the dataDictionary is not necessary, i was just demonstrating how datastore looks)

You can add them to the top of your script. And for the getData, saveData functions i would put them below all my variables(but hey that’s just how i format my scripts), any where readable would be good

2 Likes

Thanks! I’ll keep it in mind and if something doesn’t work I’ll reply back on this post

I’ve modified the script and now it looks like this:

local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")

local function loadData(datastore, key)
	local success, data = pcall(function()
		return datastore:GetAsync(key)
	end

	return data
end

local function saveData(datastore, key, value)
	local success, err = pcall(function() -- "pcall stands for protected call"
		datastore:SetAsync(key, value)
	end)

	if err then print(err) end
end

local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player 
	
	game.Players.PlayerRemoving:Connect(function(player)
		local jumps = player.leaderstats.Jumps

		saveData(jumpsDatastore, player.UserId, jumps.Value) 
	end
end



except now it doesn’t work and gives me the following errors in output:
image

image

The end needs to have a closing parentheses because we put a pcall and a function which equals to 2 parentheses end)

(apologies for the missing closing parentheses, i was tired)

And this, i was expecting you to put it inside the PlayerAdded event as you did it in the original script

(apologies for the missing closing parentheses, i was tired)

No worries man!


Thanks for making it more clear on how Data Store works, it seems to work now!

Np! goodluck on your development adventure!

1 Like