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

DataStore works fine when only one person is ingame, but changes everyone's values with multiple?

Asked by
Vid_eo 126
5 years ago

This datastore always works with the money, by the way. My issue is that the values in ReplicatedStorage change whenever someone else joins the server, and that replicates to all the clients.

LocalScript inside of Dances folder:

local loadAnimEvent = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("loadAnimEvent")

loadAnimEvent.OnClientEvent:connect(function(plr)
    print("dances loadin'")
    local plr = game.Players.LocalPlayer
    for i,e in pairs (game.ReplicatedStorage.danceValues:GetChildren()) do
        if e:IsA("BoolValue") and e.Value == true then
            print(e)
            for i,v in pairs (plr.PlayerGui.Dances:GetChildren()) do
                if v:IsA("TextButton") and v.Name == e.Name then
                    v:Clone().Parent = plr.PlayerGui.dancesGui.dancesFrame.danceFrame.danceScrolling
                end
            end
        end
    end
end)

DataStore script:

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("danceSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")

game:GetService("Players").PlayerAdded:Connect(function(Player) --When the player joins the game, load value.

    local Stats = Instance.new('Folder')
    Stats.Name = "leaderstats"

    local Money = Instance.new('NumberValue', Stats)
    Money.Name = "Money"

    Stats.Parent = Player


    --load data
    local moneyData;
    local dancesData;

    local CanSave = Instance.new('BoolValue', Player)

    CanSave.Name = "CanSaveData"
    CanSave.Value = true

    local DataFetchSuccess, ErrorMessage = pcall(function()
        moneyData = ds2:GetAsync(tostring(Player.UserId))
        dancesData = ds:GetAsync(tostring(Player.UserId))
    end)
    if DataFetchSuccess then 
        if moneyData ~= nil then --This could be where the problem is
            print("Money save found")
            Money.Value = moneyData
        else
        print("No money save found.")
            Money.Value = 0 -- Where money value is changed to 0
        end
        --print(Money.Value)
        print("k")
        if dancesData ~= nil then
            print("k")
            print(dancesData)
            for i,v in pairs(dancesData) do
                local Dance = game.ReplicatedStorage.danceValues:FindFirstChild(v)
                if dancesData then
                    Dance.Value = true
                    game.ReplicatedStorage:WaitForChild("Remotes").loadAnimEvent:FireClient(Player)
                end         
            end
        end
    else
        Player.CanSaveData.Value = false
        Player:Kick("Your data failed to load! Please rejoin.")
    end
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player) -- When the player leaves the game, data must save.
    if Player.CanSaveData.Value == false then return end --There could also be a problem with saving the money

    local moneyData = Player.leaderstats.Money.Value
    local dancesToSave = {}

    print(moneyData)
    print(dancesToSave)

    for i,v in pairs (game:GetService("ReplicatedStorage").danceValues:GetChildren()) do
        if v:IsA("BoolValue") and v.Value == true then
            table.insert(dancesToSave, v.Name)
            print(v.Name)
        end
    end

    local DataWriteSuccess, ErrorMessage = pcall(function()
        print("Saving "..Player.Name.."s stats")
        ds:SetAsync(tostring(Player.UserId), dancesToSave)
        print("ok0")
        ds2:SetAsync(tostring(Player.UserId), moneyData)
        print("ok")
    end)

    if not DataWriteSuccess then
        local Retry_Count = 0

        while Retry_Count < 6 do
            wait(60)
            local Succeed, Error = pcall(function()
        print("Attempting to save "..Player.Name.."'s stats.")
                ds:SetAsync(tostring(Player.UserId), dancesToSave)
                ds2:SetAsync(tostring(Player.UserId), moneyData)
            end)
            if Succeed then print("Saved "..Player.Name.."'s stats") break end
            Retry_Count = Retry_Count + 1
        end
    end
end)

Could this be fixed if I replicated the Folder with the dances in ReplicatedStorage to the player whenever the player joins? And then refer to the player instead of ReplicatedStorage?

Thanks.

0
ReplicatedStorage is "a container whose contents are replicated **to the server and all clients**. If you mean to store and change data to your client (locally), you should try something else. SulaymanArafat 230 — 5y
0
I suggest you use the same datastore but different keys to store different values. Operation_Meme 890 — 5y

1 answer

Log in to vote
2
Answered by
Dog2puppy 168
5 years ago

This appears to be happening because every client is using the same "dance values" that are in Replicated Storage. To fix this, just make new "dance values" for each player.

0
Would it also work if I automatically put the Dance values into the Player whenever they joined? And then accessed those dance values instead of the ones in ReplicatedStorage? Vid_eo 126 — 5y
0
Yes. Dog2puppy 168 — 5y
Ad

Answer this question