#Cant save leaderstats

89 messages · Page 1 of 1 (latest)

visual wigeon
#

I tried to save leaderstats with multiple variants of code and even asked some friends to help, but nothing seems to work, api is turned on and everything should work beacuse i dont even get an error, but after i rejoining the leaderstats always shows the value of money and income as 0

rain sage
#

You have to do it this way:

local success, errorMsg = pcall(function()
  Save1:SetAsync(Player.UserId, Player.leaderstats.Money.Value)
  Save2:SetAsync(Player.UserId, Player.leaderstats.Income.Value)
end)

if not success then
  warn(errorMsg)
end
visual wigeon
#

ServerScriptService.leaderstats:21: attempt to index nil with 'UserId'

rain sage
#

Did you remove game.Players.PlayerRemoving

visual wigeon
rain sage
#

Don't

rain sage
visual wigeon
#
    local success, errorMsg = pcall(function()
        Save1:SetAsync(Player.UserId, Player.leaderstats.Money.Value)
        Save2:SetAsync(Player.UserId, Player.leaderstats.Income.Value)
    end)

    if not success then
        warn(errorMsg)
    end
end)
#

like this?

#

if yes that dosent seem to work

rain sage
#

Why not

#

Is there an error?

visual wigeon
#

idk

#

nop[e

#

just dosent save

visual wigeon
minor adderBOT
#

studio** You are now Level 1! **studio

rain sage
#

Let me check rq.

visual wigeon
#

alr

rain sage
#

Everything works for me bro.

#
-- script.Parent = game.ServerScriptService
local DS = game:GetService("DataStoreService")
local save1 = DS:GetDataStore("Money")

game.Players.PlayerAdded:Connect(function(plr)
    local leadertats = Instance.new("Folder", plr)
    leadertats.Name = "leaderstats"
    
    local money = Instance.new("IntValue", leadertats)
    money.Name = "Money"
    money.Value = save1:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errorMsg = pcall(function()
        save1:SetAsync(plr.UserId, plr.leaderstats.Money.Value)
    end)

    if not success then
        warn(errorMsg)
    end
end)
visual wigeon
#

strange very strange

rain sage
#

Oh wait I see now.

#

I see what I did wrong hold on.

rain sage
#

Okay I'm back.

#

I had to eat dinner.

#

Anyway it should've been done like this:

#
local DS = game:GetService("DataStoreService")
local dsStore = DS:GetDataStore("PlayerStats")

game.Players.PlayerAdded:Connect(function(plr)
    local leadertats = Instance.new("Folder", plr)
    leadertats.Name = "leaderstats"
    
    local money = Instance.new("IntValue", leadertats)
    money.Name = "Money"
    
    local income = Instance.new("IntValue", leadertats)
    income.Name = "Income"
    
    local data
    
    local success, errorMsg = pcall(function()
        data = dsStore:GetAsync("midata_"..plr.UserId)
    end)
    
    if not success then
        warn(errorMsg)
    end
    
    money.Value = data[1]
    income.Value = data[2]
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local money = plr.leaderstats.Money
    local income = plr.leaderstats.Income
    local data = {money.Value, income.Value}
    
    local success, errorMsg = pcall(function()
        dsStore:SetAsync("midata_"..plr.UserId, data)
    end)

    if not success then
        warn(errorMsg)
    end
end)
visual wigeon
#

still dosent seem to work

rain sage
# visual wigeon

Are you changing the values here on the client or on the server

visual wigeon
#

server

rain sage
visual wigeon
#

but first time i put the script i had an error

#

ServerScriptService.leaderstats:24: attempt to index nil with number

#

but at the second launch it dissapeard

rain sage
#

Oh right, we forgot to check if the data even exists.

#
local DS = game:GetService("DataStoreService")
local dsStore = DS:GetDataStore("PlayerStats")

game.Players.PlayerAdded:Connect(function(plr)
    local leadertats = Instance.new("Folder", plr)
    leadertats.Name = "leaderstats"
    
    local money = Instance.new("IntValue", leadertats)
    money.Name = "Money"
    
    local income = Instance.new("IntValue", leadertats)
    income.Name = "Income"
    
    local data
    
    local success, errorMsg = pcall(function()
        data = dsStore:GetAsync("midata_"..plr.UserId)
    end)
    
    if not success then
        warn(errorMsg)
    end
    
    if data then
        money.Value = data[1]
        income.Value = data[2]
    else
        money.Value = 0
        income.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local money = plr.leaderstats.Money
    local income = plr.leaderstats.Income
    local data = {money.Value, income.Value}
    
    local success, errorMsg = pcall(function()
        dsStore:SetAsync("midata_"..plr.UserId, data)
    end)

    if not success then
        warn(errorMsg)
    end
end)
#

Check

visual wigeon
#

i think its another script that dosent let the save data to be saved but idk what script

finite stump
visual wigeon
finite stump
#
  1. roblox servers have broke (not this case)
#
  1. You havent enabled API services (if not then go to settings or search a yt vid)
#
  1. You are changing the Money and Income value through the client side
finite stump
#

When you open to test the game

#

you need to press Client on top right

visual wigeon
#
local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 170651464
local button = script.Parent
local obuchenie = true
local function clicked()
    if obuchenie == true then
        button.Parent:FindFirstChild("Tutorial"):Destroy()
        obuchenie = false
    end
    local sound = button:FindFirstChild("meow"):Clone()
    sound.Parent = workspace.Trash
    sound:Play()
    task.wait(1.35)
    sound:Destroy()
end
script.Parent.MouseButton1Click:Connect(function()
    if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
        Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 2
    else
        Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 1
    end
end)
script.Parent.MouseButton1Click:Connect(function()
    game.Workspace.Sound1:Play()
end)```
#

script for the click

finite stump
#

Try to change it without those first

visual wigeon
#

alr

finite stump
#

Open the test, switch to server side and change value from there first

finite stump
visual wigeon
minor adderBOT
#

studio** You are now Level 2! **studio

rain sage
finite stump
#

Because he stores values in the client but never replicates them to the server which cant save any data

visual wigeon
rain sage
finite stump
rain sage
rain sage
finite stump
#

he would have to use a remote event

visual wigeon
#

i hate events

rain sage
finite stump
rain sage
finite stump
visual wigeon
#

ill go learn them then learn how i can change from client to server

#

ill give an update if it works or dosent

rain sage
#

Sure.

finite stump
visual wigeon
finite stump
#

Server handles data, not the client

#

Because data is sent to roblox servers which store the data

visual wigeon
#

horray it works

#

i just have to change the income and the mega click stuff too and its done

#

thanks guys:)