Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My DataStore is sending the same data to all players, what's happening?

I'm working on a game where the player collects papers, and it has an autobackup in workspace for cloud storage every 30 seconds:

Players = game.Players
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("Papers")

while true do
	wait(15)
	for _,v in pairs(Players:GetChildren()) do
		local character = v.Character
		if character then
			local player = Players:GetPlayerFromCharacter(character)
			print (player)
			local papers = player.leaderstats:WaitForChild("Papers Collected").Value
			experienceStore:SetAsync(player, papers) --sync papers
		end
	end
end

The problem is whenever a player joins they, for some reason, get the same number of papers as the last backed up player automatically set, regardless of if they've played before and had a different number. Here's the sync:

local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("Papers")

function onPlayerEntered(player)
	local success, currentpapers = pcall(function()
		return experienceStore:GetAsync(player)
	end)
	player.leaderstats:WaitForChild("Papers Collected").Value = currentpapers
end

game.Players.PlayerAdded:connect(onPlayerEntered)

That one is in serverscriptservice. Any clue what's going on?

1 comment

0

Btw, make sure to use the task.wait() function instead, it is much reliable and faster and the old wait function will get deprecated

1 answer

✓ Accepted answer

Hello, cauctaus!

The problem is that you are trying to use a Instance object when saving/loading the data. I recommend changing it to use the UserId

Script that's in the workspace:

Players = game.Players
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("Papers")

while true do
    wait(15)
    for _,v in pairs(Players:GetChildren()) do
        local character = v.Character
        if character then
            local player = Players:GetPlayerFromCharacter(character)
            print (player)
            local papers = player.leaderstats:WaitForChild("Papers Collected").Value
            experienceStore:SetAsync(player.UserId, papers) --sync papers
        end
    end
end

Script that's in ServerScriptService

local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("Papers")

function onPlayerEntered(player)
    local success, currentpapers = pcall(function()
        return experienceStore:GetAsync(player.UserId)
    end)
    player.leaderstats:WaitForChild("Papers Collected").Value = currentpapers
end

game.Players.PlayerAdded:connect(onPlayerEntered)

Useful Links:

https://developer.roblox.com/en-us/articles/Data-store

https://developer.roblox.com/en-us/api-reference/class/Instance


If this answers your question, please don't forget to mark this as the Accepted Answer, it helps a lot

If it doesn't answer your question, please leave a commend and i'll get back to you as soon as I can

This discussion is preserved as it was. New answers, comments, and votes are closed.