So I've set up a datatore. I have three scripts managing it - One that updates when a player joins, one that updates when a player's credit value is changed, and one that updates when a player leaves (This may not work properly, I know, it's just there just in case.)

The datastore itself works perfectly fine. It inserts a credit value into every player. If I change that value in the developer console, leave, and come back, it works fine.

My problem is that I do not know how I am going to incorporate it into my main game script. When a round ends, I want to reward only the players who are still "active" with 15 credits.

I tried getting something up and running all day yesterday. I just could not figure it out and I kept breaking the script.

How would I go about doing this?

My DataStore scripts:

CREDIT REALTIME UPDATE:

local datastore = game:GetService("DataStoreService"):GetDataStore("countdowncreditservice")
game.Players.PlayerAdded:connect(function(player)
	local plyrcredits = player.statistics.credits
	plyrcredits.Changed:connect(function()
			local key = "plyr-"..player.userId
			local savedcredits = datastore:GetAsync(key)
			if savedcredits then
				plyrcredits.Value = savedcredits[1]
			end
	end)
end)

PLAYER JOIN UPDATE:

local datastore = game:GetService("DataStoreService"):GetDataStore("countdowncreditservice")

game.Players.PlayerAdded:connect(function(player)
	local statistics = Instance.new("IntValue",player)
	statistics.Name = "statistics"
	local credits = Instance.new("IntValue")
	credits.Parent = statistics
	credits.Name = "credits"
	
	local key = "plyr-"..player.userId
	
	local savedcredits = datastore:GetAsync(key)
	
	if savedcredits then
		credits.Value = savedcredits[1]
	else
		local savedvalue = {credits.Value}
		datastore:SetAsync(key, savedvalue)
	end
	
end)

PLAYER LEAVE UPDATE:

local datastore = game:GetService("DataStoreService"):GetDataStore("countdowncreditservice")

game.Players.PlayerRemoving:connect(function(player)
	local key = "plyr-"..player.userId
	local savedvalue = {player.statistics.credits.Value}
	datastore:SetAsync(key, savedvalue)
end)

Your help would be greatly appreciated. Thanks!

1 comment

0

Well for starters, I wouldn't use the realtime update. DataStores have a limitation where you can only add to or request request them every 60 seconds plus 10 seconds per player. I recommend you update their datastores every 30 seconds or so.