Implementing DataStore2 - Am I doing it right?

Hey Guys,
I’m trying to make the switch to DataStore2 and wanted to know if I’m doing it right. Here is some sample code from a collectible coin i have that gets cloned throughout the game.

It seems to work ok, but now I’m wondering if i should be making an event to increment the DataStore2 instead as there will be others scripts that will need to update it too.

Anyway, I guess I’m trying to follow best practices here, so any advice would be appreciated.

(BTW: The main controller script is pretty standard from the DataStore2 documentation so no surprises there)

local DataStore2 = require(1936396537)

local db = true

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			script.Parent.Decal1.Transparency = 1
			script.Parent.Decal2.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local goldStore = DataStore2("Gold", player)
    		        goldStore:Increment(1) -- Give them 1 gold

--			player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 1


			wait(.5)
			script.Parent:Destroy()
		end
	end
end)

What exactly does this mean?

And do you have another script in ServerScriptService that updates the leaderboard?

I mean other scripts for the shop and stuff like that.
Btw: That script is in the coin, although isn’t a local script so i assume that is safe.

Using :Increment in multiple scripts is a fine practice. If what you have now is working flawlessly, than continue using that method.

Ok, great because I don’t know a lot about events yet anyway. Thanks for your help.

1 Like

Your implementation looks good! Just don’t forget to combine your keys! https://kampfkarren.github.io/Roblox/guide/gotchas/ In my game, I have an update function to update local values whenever the datastore2 one changes. This shouldn’t matter as long as your always checking the saved value instead of a local value.

Looking good!

Great Advice! I have the following line in my main datacontroller script

DataStore2.Combine("MasterKey2", "Gold", "Gems", "Wins")

And I assume that’s the only play I need to reference it?

Yep, pretty much. As Kampfkarren says, in DataStore2, this is a built-in feature.

Checks out all fine to me.

One nitpick though, you might as well use GetPlayerFromCharacter before the first if statement. Then as your conditional you can just put player (Instances evaluate to true).

Also, you can combine the two if statements together, no need for == true as well because if db is true then it’ll pass through fine.

For example:

local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player and db then
    --// Code
end

I’m mainly suggesting this because if a NPC hits then DataStore2 will surely error.

Wow! Thank-you for the tips. Admittedly, I hadn’t reviewed the code since the change but what you say totally makes sense. Thank-you.

Just taking small steps, and learning as i go, so this sort of feedback is invaluable.