#Leaderstat not saving

1 messages · Page 1 of 1 (latest)

plain ibex
#

So I got a leaderstat but it is not savong and I also added a script which should save it but its not.

#

saving script:

-- Make sure "Enable Studio Access To API Services" is on in Game Settings! (OR IT WON'T WORK) --
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)

if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)

local function Save(player)
local SavedData = {}
for _, v in pairs(player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end

local success, errormessage = pcall(function()
Saver:SetAsync(tostring(player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)

#

leaderstats script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

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

local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Value = 0
Money.Parent = leaderstatsFolder

local DriverPoints = Instance.new("IntValue")
DriverPoints.Name = "XP"
DriverPoints.Value = 0
DriverPoints.Parent = leaderstatsFolder

end)

plain ibex
#

just the disconnection output

rotund turtle
#

here

#

it is for_, v

#

not for , v

plain ibex
#

should I change it to _, v?

rotund turtle
#

just add a underscore

#

before ,

plain ibex
#

okay thank you :D

rotund turtle
#

see if it will work

#

wait

#

there is also this one

#

@plain ibex

#

before the leaderstats script

#

u need to change this to for _, v too

plain ibex
#

alright ty :)

#

they both got "_" I cant find any without it

rotund turtle
#

still not working?

plain ibex
#

nope

rotund turtle
#

hmm

#

let me read the script carefully rq

plain ibex
daring edge
#

Maybe instead of SetAsync use UpdateAsync? I heard that its overall better and SetAsync sometimes didn't save data for me as well.

#

Code would look like this if I am not wrong:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
    local Data = nil
    local success, errormessage = pcall(function()
        Data = Saver:GetAsync(tostring(player.UserId))
    end)

    if success then
        if Data then
            for i, v in pairs(Data) do
                player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
            end
        end
    else
        error(errormessage)
    end
end)

local function Save(player)
    local SavedData = {}
    for _, v in pairs(player.leaderstats:GetChildren()) do
        SavedData[v.Name] = v.Value
    end

    local success, errormessage = pcall(function()
        Saver:UpdateAsync(tostring(player.UserId), function()
            return SavedData
        end)
    end)
    if not success then
        error(errormessage)
    end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
    for _, v in pairs(Players:GetPlayers()) do
        Save(v)
    end
end) ```
plain ibex
daring edge
plain ibex
sudden krakenBOT
#

studio** You are now Level 6! **studio

daring edge
#

hm

#

Huh extremly strange. I just copied the exact scripts into my game and it worked perfectly fine. Are you really sure that you got the Api enabled?

plain ibex
#

I enabled every API except the image api

daring edge
#

and your game is published to roblox?

plain ibex
#

yes

#

I published it again i will test it

daring edge
#

alr

plain ibex
#

still not working

#

I just got this in my output

Failed to parse secrets: Can't parse JSON - Edit

daring edge
#

for which line?

plain ibex
#

I dont know I think its from the secrets thing in the game settings under Security

#

it did not say any line

daring edge
#

So I added a lot of prints to the script now to check where the problem is:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
    local Data = nil
    local success, errormessage = pcall(function()
        Data = Saver:GetAsync(tostring(player.UserId))
        print("LeaderStatsDataStore | Getting data")
    end)

    if success then
        print("LeaderStatsDataStore | Getting data was a success")
        if Data then
            print("LeaderStatsDataStore | data found")
            for i, v in pairs(Data) do
                player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
                print("LeaderStatsDataStore | Set ".. i .. ": " .. v)
            end
            print("LeaderStatsDataStore | Setting leaderstats done")
        end
    else
        print("LeaderStatsDataStore | Getting data failed: Reason below")
        error(errormessage)
    end
end)

local function Save(player)
    print("LeaderStatsDataStore | Save function called")
    local SavedData = {}
    print("LeaderStatsDataStore | Collecting data")
    for _, v in pairs(player.leaderstats:GetChildren()) do
        SavedData[v.Name] = v.Value
        print("LeaderStatsDataStore | Collected ".. v.Name .. ": " .. v.Value)
    end
    print("LeaderStatsDataStore | Data collected")

    local success, errormessage = pcall(function()
        Saver:UpdateAsync(tostring(player.UserId), function()
            print("LeaderStatsDataStore | Setting DataTable")
            return SavedData
        end)
    end)
    if success then
        print("LeaderStatsDataStore | Data saved Successfully")
    end
    if not success then
        error(errormessage)
    end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
    for _, v in pairs(Players:GetPlayers()) do
        Save(v)
    end
end)
#

Just show me the output after joining and after leaving

plain ibex
#

Server - DataSave:18
00:13:59.358 LeaderStatsDataStore | Setting leaderstats done - Server - DataSave:20

00:14:16.128 LeaderStatsDataStore | Save function called - Server - DataSave:29
00:14:16.128 LeaderStatsDataStore | Collecting data - Server - DataSave:31
00:14:16.128 LeaderStatsDataStore | Collected Money: 0 - Server - DataSave:34
00:14:16.128 LeaderStatsDataStore | Collected DriverPoints: 0 - Server - DataSave:34
00:14:16.128 LeaderStatsDataStore | Data collected - Server - DataSave:36
00:14:16.482 LeaderStatsDataStore | Setting DataTable - Server - DataSave:40
00:14:16.699 LeaderStatsDataStore | Data saved Successfully - Server - DataSave:45

thanks for your help btw :)

daring edge
#

is that all?

plain ibex
#

yes

daring edge
#
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

print("Test")

Players.PlayerAdded:Connect(function(player)
    local Data = nil
    local success, errormessage = pcall(function()
        Data = Saver:GetAsync(tostring(player.UserId))
        print("LeaderStatsDataStore | Getting data")
    end)

    if success then
        print("LeaderStatsDataStore | Getting data was a success")
        if Data then
            print("LeaderStatsDataStore | data found")
            for i, v in pairs(Data) do
                player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
                print("LeaderStatsDataStore | Set ".. i .. ": " .. v)
            end
            print("LeaderStatsDataStore | Setting leaderstats done")
        end
    else
        print("LeaderStatsDataStore | Getting data failed: Reason below")
        error(errormessage)
    end
end)

local function Save(player)
    print("LeaderStatsDataStore | Save function called")
    local SavedData = {}
    print("LeaderStatsDataStore | Collecting data")
    for _, v in pairs(player.leaderstats:GetChildren()) do
        SavedData[v.Name] = v.Value
        print("LeaderStatsDataStore | Collected ".. v.Name .. ": " .. v.Value)
    end
    print("LeaderStatsDataStore | Data collected")

    local success, errormessage = pcall(function()
        Saver:UpdateAsync(tostring(player.UserId), function()
            print("LeaderStatsDataStore | Setting DataTable")
            return SavedData
        end)
    end)
    if success then
        print("LeaderStatsDataStore | Data saved Successfully")
    end
    if not success then
        error(errormessage)
    end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
    for _, v in pairs(Players:GetPlayers()) do
        Save(v)
    end
end)

Try again?

plain ibex
#

00:17:01.043 Test - Server - DataSave:5
00:17:01.854 LeaderStatsDataStore | Getting data - Server - DataSave:11
00:17:01.854 LeaderStatsDataStore | Getting data was a success - Server - DataSave:15
00:17:01.854 LeaderStatsDataStore | data found - Server - DataSave:17
00:17:01.854 LeaderStatsDataStore | Set DriverPoints: 0 - Server - DataSave:20
00:17:01.855 LeaderStatsDataStore | Set Money: 0 - Server - DataSave:20
00:17:01.855 LeaderStatsDataStore | Setting leaderstats done - Server - DataSave:22
00:17:11.081 LeaderStatsDataStore | Save function called - Server - DataSave:31
00:17:11.081 LeaderStatsDataStore | Collecting data - Server - DataSave:33
00:17:11.081 LeaderStatsDataStore | Collected Money: 0 - Server - DataSave:36
00:17:11.081 LeaderStatsDataStore | Collected DriverPoints: 0 - Server - DataSave:36
00:17:11.081 LeaderStatsDataStore | Data collected - Server - DataSave:38
00:17:11.439 LeaderStatsDataStore | Setting DataTable - Server - DataSave:42
00:17:11.647 LeaderStatsDataStore | Data saved Successfully - Server - DataSave:47

#

I changed the value to 100 I think but it still is saying 0

#

I also added buttons which adds +10 value

daring edge
#
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

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

    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Value = 20
    Money.Parent = leaderstatsFolder

    local DriverPoints = Instance.new("IntValue")
    DriverPoints.Name = "XP"
    DriverPoints.Value = 30
    DriverPoints.Parent = leaderstatsFolder

end) 

Hm... Replace this with your leaderstats script (Or disable your leaderstats script and create a new one with this code)

plain ibex
#

00:20:57.665 Test - Server - DataSave:5
00:20:58.437 LeaderStatsDataStore | Getting data - Server - DataSave:11
00:20:58.437 LeaderStatsDataStore | Getting data was a success - Server - DataSave:15
00:20:58.437 LeaderStatsDataStore | data found - Server - DataSave:17
00:20:58.437 LeaderStatsDataStore | Set Money: 20 - Server - DataSave:20
00:20:58.437 LeaderStatsDataStore | Set XP: 30 - Server - DataSave:20
00:20:58.437 LeaderStatsDataStore | Setting leaderstats done - Server - DataSave:22
00:21:00.828 LeaderStatsDataStore | Save function called - Server - DataSave:31
00:21:00.828 LeaderStatsDataStore | Collecting data - Server - DataSave:33
00:21:00.828 LeaderStatsDataStore | Collected Money: 20 - Server - DataSave:36
00:21:00.828 LeaderStatsDataStore | Collected XP: 30 - Server - DataSave:36
00:21:00.828 LeaderStatsDataStore | Data collected - Server - DataSave:38
00:21:01.029 LeaderStatsDataStore | Setting DataTable - Server - DataSave:42
00:21:01.246 LeaderStatsDataStore | Data saved Successfully - Server - DataSave:47

daring edge
#

now change the values back to 0

plain ibex
#

ingame via explorer>players or in the script?

daring edge
plain ibex
#

alr

#

00:22:54.430 LeaderStatsDataStore | Save function called - Server - DataSave:31
00:22:54.430 LeaderStatsDataStore | Collecting data - Server - DataSave:33
00:22:54.430 LeaderStatsDataStore | Collected Money: 20 - Server - DataSave:36
00:22:54.430 LeaderStatsDataStore | Collected XP: 30 - Server - DataSave:36
00:22:54.430 LeaderStatsDataStore | Data collected - Server - DataSave:38
00:22:54.768 LeaderStatsDataStore | Setting DataTable - Server - DataSave:42
00:22:54.980 LeaderStatsDataStore | Data saved Successfully - Server - DataSave:47

daring edge
#

did it not set the leaderstats?

plain ibex
#

I dont know
I changed it back to 0 in the leaderstatsscript

#

`local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Value = 0
Money.Parent = leaderstatsFolder

local DriverPoints = Instance.new("IntValue")
DriverPoints.Name = "XP"
DriverPoints.Value = 0
DriverPoints.Parent = leaderstatsFolder`

daring edge
#

can you check via the explorer on the server if the IntValues have changed ?

plain ibex
#

sure

#

they still are on 20 and 30

daring edge
#

that is good

#

can i see your script where you change these values

plain ibex
#

`local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

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

local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Value = 0
Money.Parent = leaderstatsFolder

local DriverPoints = Instance.new("IntValue")
DriverPoints.Name = "XP"
DriverPoints.Value = 0
DriverPoints.Parent = leaderstatsFolder

end) `

daring edge
plain ibex
#

`local textButton = script.Parent
local Players = game:GetService("Players")

textButton.MouseButton1Click:Connect(function()
local player = Players.LocalPlayer

if player then
local leaderstats = player:WaitForChild("leaderstats")
local driverPoints = leaderstats:WaitForChild("DriverPoints")

driverPoints.Value = driverPoints.Value + 10

local originalText = textButton.Text
textButton.Text = "+10 Points!"
task.wait(0.5)
textButton.Text = originalText

end
end)`

#

thats the XP value in leaderstats I just gave it a new name

daring edge
#

Alr i know what the problem is.

#

How much do you know about client and server?

plain ibex
#

client is what happens for the player only and server for everyone in the game

#

thats what I know

daring edge
#

Exactly

#

So when you change the driverPoints.Value in your local script

#

that only happens for your client

plain ibex
#

oh true

#

thank you i did not notice haha

daring edge
#

No problem.

plain ibex
#

thanks for your help :D

daring edge
#

Ay man No Problem. Just ping me / dm me if you run into another problem with your DataStores 👍

plain ibex
#

have a nice day 😄

daring edge
#

you too