Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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?

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 h8run 30 — 3y

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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

Ad

Answer this question