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

Why is my DataStore2 saving random values into my Coins?

Asked by 3 years ago
Edited 3 years ago

So after a while of not saving Coins in my game, I decided to save them using DataStore 2. I was already saving another currency so no big deal, should be easy. So in this game, you get usually 5 coins after every floor but I can't just make the data stores save with 5 more coins since sometimes you'd get more like a boss floor for example or anything else. So I need to do some math to get it to increment any Coins the player gets.

This is what I did. (You shouldn't require more code than this)

            local DataStoreService = game:GetService("DataStoreService")
local GameDataStore = DataStoreService:GetDataStore("GameDataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerScriptService = game:GetService("ServerScriptService")
local workspace = game:GetService("Workspace")

AlivePlayers = {}

Players.PlayerAdded:Connect(function(player)
    print("Player joined and is being tracked to see if they die.")
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            if table.find(AlivePlayers,player) then
                for i = 1, #AlivePlayers do
                    if AlivePlayers[i] == player then
                        table.remove(AlivePlayers, i)
                        print("A player has been removed from the Alive People list.(Hopefully)")
                    end
                end
            end
            if table.find(AlivePlayers,player) then
                print("Failed to remove a player from the Alive People list.")
                return
            end
            wait(3.5)
            player.Character:Destroy()
            player.Values.Lobby.Value = true
        end)
    end)
end)

workspace.Lobby.PlayersinLobby.Touched:Connect(function(hit)
    if hit.parent:FindFirstChild("Humanoid") and Players:GetPlayerFromCharacter(hit.parent) then
        local character = hit.parent
        local player = Players[character.Name]
        player.CameraMode = Enum.CameraMode.Classic
        player.Values.Lobby.Value = true
        if table.find(AlivePlayers,player) then
            for i = 1, #AlivePlayers do
                if AlivePlayers[i] == player then
                    table.remove(AlivePlayers, i)
                    print("Removed a player from the Alive Players list for staying in the lobby")
                end
            end
        end
    end
end)


local DataStore2 = require(ServerScriptService:WaitForChild("DataStore2"))

DataStore2.Combine("DATA", "floors2", "coins2")

local defaultValue = 0

Players.PlayerAdded:Connect(function(player)

    local Items = Instance.new("Folder")
    Items.Name = "Items"
    Items.Parent = player

    local Values = Instance.new("Folder")
    Values.Name = "Values"
    Values.Parent = player

    local Lobby = Instance.new("BoolValue")
    Lobby.Name = "Lobby"
    Lobby.Parent = Values
    Lobby.Value = true

    local SC = Instance.new("IntValue")
    SC.Name = "SC"
    SC.Parent = Items
    SC.Value = 0

    local TimeT = Instance.new("IntValue")
    TimeT.Name = "TimeT"
    TimeT.Parent = Items
    TimeT.Value = 0

    local Flash = Instance.new("IntValue")
    Flash.Name = "Flash"
    Flash.Parent = Items
    Flash.Value = 0

    local RegenCoil = Instance.new("IntValue")
    RegenCoil.Name = "RegenCoil"
    RegenCoil.Parent = Items
    RegenCoil.Value = 0

    local UltraCoil = Instance.new("IntValue")
    UltraCoil.Name = "UltraCoil"
    UltraCoil.Parent = Items
    UltraCoil.Value = 0

    local HackDevice = Instance.new("IntValue")
    HackDevice.Name = "HackDevice"
    HackDevice.Parent = Items
    HackDevice.Value = 0

    local Port = Instance.new("IntValue")
    Port.Name = "PortShop"
    Port.Parent = Items
    Port.Value = 0

    print("Player data epicly loads (hopefully it didnt corrupt)")

    local CoinsStore = DataStore2("coins2", player)
    local floorsStore = DataStore2("floors2", player)

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local Coins = Instance.new("IntValue", leaderstats)
    Coins.Name = "Coins"
    Coins.Value = CoinsStore:Get(0)

    local Floors = Instance.new("IntValue", leaderstats)
    Floors.Name = "Floors"
    Floors.Value = floorsStore:Get(0)

    local function coinUpdate(updatedValue)
        Coins.Value = CoinsStore:Get(updatedValue)

        CoinsStore:SetBackup(5)
    end

    local function floorUpdate(updatedValue)
        Floors.Value = floorsStore:Get(updatedValue)

        floorsStore:SetBackup(5)
    end

    coinUpdate(defaultValue)

    floorUpdate(defaultValue)

    CoinsStore:OnUpdate(function(coinUpdate)
    end)
    floorsStore:OnUpdate(function(floorUpdate)
end)

Players.PlayerAdded:Connect(function(player)
        local CoinsStore = DataStore2("coins2", player)
        local floorsStore = DataStore2("floors2", player)
    while true do
        wait(180)
            CoinsStore:Save()
            floorsStore:Save()
    end 
end)

game.ServerStorage.PlayerIntermission.Changed:Connect(function()
    if game.ServerStorage.PlayerIntermission.Value == 0 then
        local players = Players:GetPlayers()
        for i=1,#players do
            table.insert(AlivePlayers,players[i])
        end
        local parts = workspace.Lobby.PlayersinLobby:GetTouchingParts()
        for _, touchingPart in ipairs(parts) do
            local humanoid = touchingPart.Parent:FindFirstChild("Humanoid")
            if humanoid and humanoid.MoveDirection == Vector3.new(0,0,0) or humanoid then
                local character = humanoid.Parent
                if Players:GetPlayerFromCharacter(humanoid.Parent) then
                    local player = Players[character.Name]
                    player.CameraMode = Enum.CameraMode.Classic
                    if table.find(AlivePlayers,player) then
                        for i = 1, #AlivePlayers do
                            if AlivePlayers[i] == player then
                                table.remove(AlivePlayers, i)
                                print("Removed every player in the Lobby from the Alive Players list.")
                            end
                        end
                    end
                end
            end
        end
    else
            for i, player in pairs(AlivePlayers) do
                local CoinsStore = DataStore2("coins2", player)
                local floorsStore = DataStore2("floors2", player)
                local OldCoins = leaderstats.Coins.Value
                player.leaderstats.Floors.Value += 1
                player.leaderstats.Coins.Value += 5
                wait(1)
                local NewCoins = leaderstats.Coins.Value
                local AddedCoins = NewCoins - OldCoins
                print(AddedCoins.." Coins added to the player's data.")
                floorsStore:Increment(1,defaultValue)
                CoinsStore:Increment(AddedCoins,defaultValue)
                table.clear(AlivePlayers)
            end
        end
    end)
end)

This should work (I think?) but it doesn't. I went from 90 coins to 100, (double coins gamepass, hence why I can't just say 5 coins) sounds good right? I leave the game, join back and I have 55 Coins???? Whaaaaaaat? How did I get from 100 (NewCoins) - 90 (OldCoins) = 10 (AddedCoins) to 55??? I cannot figure this out, so if you can help please help :)

Answer this question