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

Why does leaderstats not show up in my player? (player.leaderstats)

Asked by 3 years ago
Edited 3 years ago

I'm using Datastore 2 and sometimes, when I play, I get an error: "leaderstats is not a valid member of Player "Players.Hoogidy_Boogidy" - Client - DisplayCoinsAmount:4". If you used Datastore 2 before, or watched Alvin Blox's tutorial about it, then you know that you can save data in studio by adding a Boolean Value set to true in ServerStorage. If leaderstats isn't showing up because of that, then please tell me.

I have 2 scripts. The first one is "DisplayCoinsAmount", which is the one that gave the error and the one that displays the amount of coins the player has, in a GUI. The second script is "SaveData", which is the one that creates the Folder, "leaderstats" and saves the coins for the player. I can't get Roblox studio to give me the error again. It only happened twice.

Here is my 2 scripts:

DisplayCoinsAmount:

local plr = script.Parent.Parent.Parent.Parent.Parent

game:GetService('RunService').RenderStepped:Connect(function()
    script.Parent.Text = tostring(plr.leaderstats.Coins.Value)
end)

SaveData:

local DS2 = require(1936396537)
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)
    local coinsDS = DS2('Coins', plr)
    local leaderstats = Instance.new('Folder', plr)
    local coins = Instance.new('IntValue', leaderstats)

    leaderstats.Name = 'leaderstats'
    coins.Name = 'Coins'

    local function updateCoins(updatedValue)
        coins.Value = coinsDS:Get(updatedValue)
    end

    updateCoins(DefaultValue) -- Idk what these two lines are supposed to do
    coinsDS:OnUpdate(updateCoins) -- I just copied Alvin Blox :|

    while wait() do
        coins.Value = coinsDS:Get()
    end
end)

I would appreciate if you would help! Thank you! :D

Edit:

I tried using game.Loaded, but I don't know what that does

Code:

local DS2 = require(1936396537)
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)
    local coinsDS = DS2('Coins', plr)

    repeat wait() until(game.Loaded)

    local leaderstats = Instance.new('Folder', plr)
    local coins = Instance.new('NumberValue', leaderstats)

    leaderstats.Name = 'leaderstats'
    coins.Name = 'Coins'

    local function updateCoins(updatedValue)
        coins.Value = coinsDS:Get(updatedValue)
    end

    updateCoins(DefaultValue)
    coinsDS:OnUpdate(updateCoins)

    while wait() do
        coins.Value = coinsDS:Get()
    end
end)
0
I would reocmmend using normal datastore; much simpler. PufferfishDev 49 — 3y
0
Normal datastore is long, and DS2 saves data better. Hoogidy_Boogidy 18 — 3y
0
does it show leaderstats in the player in the explorer (when you click play)? FirewolfYT_751 223 — 3y
0
Yes, but when I get the error, that means it isn't there. Hoogidy_Boogidy 18 — 3y
View all comments (7 more)
0
Maybe try using "WaitForChild("")" a few times to get the player..? ZIRFAL3 17 — 3y
0
maybe add a WaitForChild on leaderstats, maybe your client script is executing too fast before the server can load the leaderstats the8bitdude11 358 — 3y
0
Where would I add the WaitForChild? Hoogidy_Boogidy 18 — 3y
0
Replace line 4 with -- script.Parent.Text = tostring(plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value) JustinWe12 723 — 3y
0
That only gets rid of the error. But leaderstats isn't there. So, it's just waiting for leaderstats forever. Sorry for not replying sooner!!!!!!! Hoogidy_Boogidy 18 — 3y
0
What do you mean by leaderstats isn't there, is it not showing up on the top right corner of the game? JustinWe12 723 — 3y
0
it isn't showing in my player Hoogidy_Boogidy 18 — 3y

2 answers

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago

Sorry for the late answer, but this should work!

Problem: It's pretty hard to explain but the reason it didn't work was because the playerevent was declared after the variables, when it should be on top:

(Also ensure that this is in a server script (normal script) I changed your last script!

game.Players.PlayerAdded:Connect(function(plr)
local DS2 = require(1936396537)
local DefaultValue = 0

    local coinsDS = DS2('Coins', plr)

    repeat wait() until(game.Loaded)

    local leaderstats = Instance.new('Folder', plr)
    local coins = Instance.new('NumberValue', leaderstats)

    leaderstats.Name = 'leaderstats'
    coins.Name = 'Coins'

    local function updateCoins(updatedValue)
        coins.Value = coinsDS:Get(updatedValue)
    end

    updateCoins(DefaultValue)
    coinsDS:OnUpdate(updateCoins)

    while wait() do
        coins.Value = coinsDS:Get()
    end
end)
1
Thx for trying! Hoogidy_Boogidy 18 — 3y
0
message me when you get the correct answer! JesseSong 3916 — 3y
Ad
Log in to vote
-1
Answered by 3 years ago
Edited 3 years ago

put wait(3) on line 1 it will wait 3 seconds so it will be loaded and wont make error bcs it will find it. this is script for saving folders. if someone need normal saving for folders :)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

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

    local money = Instance.new("NumberValue", folder)
    money.Name = "Money"

    local playerUserId = "Player_"..player.UserId
    local data = playerData:GetAsync(playerUserId)
    if data then
        money.Value = data["Money"]
        -- here u can add more values from top
    else
        money.Value = 0
    end
end

local function create_table(player)
    local player_stats = {}
    for _, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function onPlayerExit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserId = "Player_"..player.UserId
        playerData:SetAsync(playerUserId, player_stats)
    end)
    if not success then
        warn("Could not save data")
    end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
0
I'm using Datastore 2 Hoogidy_Boogidy 18 — 3y
0
but wait should help moneyislands1 51 — 3y
0
what if the player didn't fully load? Hoogidy_Boogidy 18 — 3y
0
Sorry for not replying sooner Hoogidy_Boogidy 18 — 3y

Answer this question