Datastore too many requests

Hello,

We’re making a system using Coins… the aim is to touch a model and then get coins from it, but there is hundreds of them.

Now, each one relies on touching it for it to give the player a coin, none of them work obviously because the datastore is full up with hundreds of those requests.

I’m just wondering if there is anything I or we could do to maybe rectify this or maybe even limit the amount of the parts at first spawn?

Anything is appreciated, thanks…

Updating the datastore each time you do anything in a game (picking up coins in ur example) is a very bad idea, either save it somewhere temporarily in your game, and save it when the player leaves, or save every players data in the game every x minutes

1 Like

thank you so much ill pass this on because I have no idea how to implement this…

Where should I change it instead? I am the scripter for this, btw. Here it the script for the coin:

local DataStoreService = game:GetService("DataStoreService")
local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore")

local RespawnTime = 180

function savePlayerData(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local coins = leaderstats:FindFirstChild("Hay")
		if coins then
			CoinsDataStore:SetAsync(player.UserId .. "Hay", coins.Value)
		end
	end
end

function loadPlayerData(player)
	local success, result = pcall(function()
		return CoinsDataStore:GetAsync(player.UserId .. "Hay")
	end)
	if success and result ~= nil then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local coins = leaderstats:FindFirstChild("Coins")
			if coins then
				coins.Value = result
			end
		end
	end
end

function onTouch(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		local leaderstats = player:FindFirstChild("leaderstats")
		if not leaderstats then

			return
		end
		local coins = leaderstats:FindFirstChild("Hay")
		if coins then
			coins.Value = coins.Value + 1 
		end
		script.Parent.CanTouch = false 
		script.Parent.Transparency = 1
		script.Parent.Parent.Hay.Attachment.Rays.Enabled = false
		script.Parent.Parent.Hay.Attachment.Glow.Enabled = false
		script.Parent.Parent.Hay.PointLight.Enabled = false
		wait(RespawnTime)
		script.Parent.CanTouch = true
		script.Parent.Transparency = 0
		script.Parent.Parent.Hay.Attachment.Rays.Enabled = true
		script.Parent.Parent.Hay.Attachment.Glow.Enabled = true
		script.Parent.Parent.Hay.PointLight.Enabled = false

	end
end

function onPlayerRemoving(player)
	savePlayerData(player)
end

function onPlayerAdded(player)
	loadPlayerData(player)
end

script.Parent.Touched:Connect(onTouch)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
game.Players.PlayerAdded:Connect(onPlayerAdded)

that looks fine, you are only saving now when the player leaves / joins (considering you dont have other scripts doing it)

Why don’t you just connect the data functions directly to the event?

Ex:

game.Players.PlayerAdded:Connect(loadPlayerData)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.