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

DataStore doesnt save... Help is much appreciated!?

Asked by 2 years ago

Hi, ive got this leaderstat script with a database to save my coins, gems and rebirths.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(Player)
    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"

    local val1 = Instance.new("StringValue",Player)
    val1.Name = 'GotPet'
    val1.Value = ''

    local val2 = Instance.new("StringValue",Player)
    val2.Name = 'OpenValue'
    val2.Value = ''

    local Gems = Instance.new("IntValue", leaderstats)
    Gems.Name = "Gems"
    Gems.Value = 0

    local Rebirths = Instance.new("IntValue", leaderstats)
    Rebirths.Name = "Rebirths"
    Rebirths.Value = 0

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

    local playerUserId = "Player_" .. Player.UserId  --Gets player ID

    local Success, Result = pcall(function()
        return playerData:GetAsync(playerUserId)
    end)

    if Result then
        print("Data found!")

        Coins.Value = Result[1] -- // Since in the saving part, Coins are the one that is set as the first value of the table, we set Coins as [1]
        Rebirths.Value = Result[2]
        Gems.Value = Result[3]
    else
        print("DataStore working but No data found")
        warn(Result)
    end

end

local function onPlayerExit(player)  --Runs when players exit



    local success, Err = pcall(function()
        print("Data Saved successfully")

        local playerUserId = "Player_" .. player.UserId

        playerData:SetAsync(playerUserId, {player.leaderstats.Coins.Value, player.leaderstats.Rebirths.Value, player.leaderstats.Gems.Value}) -- Now set as a table instead of individually

    end)



    if not success then
        warn(Err)
        warn('Could not save data!')
    end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

It dont work :( Now i have another script that adds gems and coins, could it be stopping it from working and how could i fix it?

local player = game.Players.LocalPlayer
local Codes = script.Parent:WaitForChild("holder"):WaitForChild("Codes"):GetChildren()
local CodeText = script.Parent.holder.__Codes.__EnterCode:WaitForChild("EnterCode")

local Cash = player:WaitForChild("leaderstats"):WaitForChild("Coins")
local Gems = player:WaitForChild("leaderstats"):WaitForChild("Gems")

script.Parent.holder.__Codes.__Confirm:WaitForChild("Confirm").MouseButton1Click:connect(function()
    for i, v in pairs(Codes) do
        if CodeText.Text == v.Name then
            if v.Redeemed.Value == false then
                Cash.Value = Cash.Value + v.CashToGive.Value
                Gems.Value = Gems.Value + v.GemsToGive.Value
                    game.Workspace.Sounds.MoneySound:Play()
                local Trans = script.Parent.holder.__Codes.Success
                Trans.TextTransparency = 0
                Trans.TextStrokeTransparency = 0
                v.Redeemed.Value = true
                wait(2.5)
                for i = 0, 1, 0.1 do
                    wait(0)
                    Trans.TextTransparency = i
                    Trans.TextStrokeTransparency = i

                end
            end
        end
    end
end)

This is just a simple twitter code gui thingy that gives gems and or coins.

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

For reference: Client is the player/localscript Server is the Server

The issue is the Twitter Code; you're using Client to give the Player Coins. You should use the Client to handle the button press, then the Server processes the transaction.

The Datastore does not work because anything that the Client input, it doesn't replicate to the Server. But anything that the Server does, it replicates to the Client. (I don't know if that makes sense) but in other words, Datastores does not work on the Client, it only accepts input from the Server.

You can search for tutorials on Youtube.

Also, if you want to test a Server script, you can use the Dev Console (F9 or /console) then press Server, then write your code in the console.

0
Thanks so much dude! User#31501 0 — 2y
Ad

Answer this question