Switching to DataStore2

Hm okay I only have a kill for money script in StarterPlayerScript, and a kill script inside zombie to give gold, and a coin script.

Could you see if you having any other data store scripts in your game that you didn’t delete?

Okay hold up let me try it again.

I do not think so though because I think it is just something about not updating it maybe when you first join? Or you might be correct because the leaderboard shows ur gold. But I am thinking maybe there is a script in the datastore that makes it not run and show the actual person their gold?

after the line that says goldStat.Value = goldData:Get() write print(goldData:Get()) I wanna know what it thinks you have

? what do you mean do you want me to show you the hwole code?

no, I want you to add that to the code, do a play test, and then show me the output

Oh to see what’s wrong? and see if it works its a way to debug

Yes, doing that should show what is wrong

I will do right now. But there is a problem. How would I know do you mean it will be underlined? Or what do you mean?


Weird in Roblox Studio I started off with the value. Let me see and try again and a few more times to make sure.

Nope only sometimes it starts with that.

I’ll just show you:

local defaultValue: number = 100 --If player has no data then their gold will be set to this value

local DataStore2 = require(1936396537)
DataStore2.Combine("MasterKey", "Gold")

game.Players.PlayerAdded:Connect(function(player)
	local goldData = DataStore2("Gold", player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local goldStat = Instance.new("IntValue", leaderstats)
	goldStat.Name = "Gold"

	local function UpdateStat()
		goldStat.Value = goldData:Get(defaultValue)
	end

	goldData:OnUpdate(function()
		UpdateStat()
	end)
	
	goldStat.Changed:Connect(function()
		if goldStat.Value > goldData:Get() then
			goldData:Set(goldStat.Value)
		end
	end)

    goldStat.Value = goldData:Get(defaultValue)
    print(goldData:Get())
end)

okay so do I test this out or what should I do? Do you mean look at the output when I run it?

Also I forgot to mention. So basically, the times the gold does not show to teh player, it shows right after you do something to the value. So if the value changes in any way like buying something or earning more, it comes and shows the value.

I see, so I’ll do some more research on the DataStore2 module and see if I can find a way to update it when you join.

Okay Thanks So Much! You are the best!

I believe I have found a fix:

local defaultValue = 100 --If player has no data then their gold will be set to this value

local DataStore2 = require(1936396537)

game.Players.PlayerAdded:Connect(function(player)
	local goldData = DataStore2("Gold", player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local goldStat = Instance.new("IntValue", leaderstats)
	goldStat.Name = "Gold"

	local function UpdateStat()
		goldStat.Value = goldData:Get(defaultValue)
	end

	goldData:OnUpdate(function()
		UpdateStat()
	end)

	goldStat.Changed:Connect(function()
		if goldStat.Value > goldData:Get() then
			goldData:Set(goldStat.Value)
		end
	end)

	goldStat.Value = goldData:Get(defaultValue)
	
end)

Also make sure you have HTTP Requests and API Services enabled!

1 Like