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

Leaderstats not a part of the player?

Asked by 5 years ago

Trying to script multiple elements for leaderstats to save through the use of a data store however, I keep coming across the same error over and over.

Here are the two scripts for the leaderstat values and the data stores. https://gyazo.com/eb8b0b6aadf9a2c4cfa7f5bb461e9ad2 https://gyazo.com/adc5b8e9b36f57286b80bcc3b64cf448

Upon playing the game only one value appears on the leaderboard. https://gyazo.com/c6194ea657384749d4b12d16381607c9

An error is returned saying leaderstats is not apart of the player. https://gyazo.com/e09c08ab446fb470c10cbc92e5c36a50

However, when checked in game leaderstats exists as part of the player. https://gyazo.com/e2ae4080484c1ece085e9bf82a9c8223

0
Looks like it may just be a timing problem(the ore value is being put into the folder which has yet to be created.) I'd either put all the leaderstat values in the same script or put a wait in the ore script to allow the leaderstats folder to be made ForeverBrown 356 — 5y
0
Seems to have fixed the issue I was having. Appreciate it. Sossiago_IV -3 — 5y

2 answers

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

Thats my Problem too but i realize i have to put :WaitForChild("leaderstats") in every Variable for Leaderstats.

Local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
Ad
Log in to vote
0
Answered by
8_zn 21
5 years ago
Edited 5 years ago

You shouldn't really be using connect as its deprecated, instead, you should be using Connect. Also, you can just put everything into 1 script, instead of having 2 scripts. This is script is more efficient instead of having 2 scripts. Your problem was not having a wait() function in the 2nd script. Hope this helps!

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("ServerSaveSystem")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Parent =  Player
    leaderstats.Name = "leaderstats"
    local Cash = Instance.new("IntValue")
    Cash.Parent = leaderstats
    Cash.Name = "Cash"
    Cash.Value = ds:GetAsync(Player.UserId) or 0
    local Ore = Instance.new("IntValue")
    Ore.Parent = leaderstats
    Ore.Name = "Ore"
    Ore.Value = ds:GetAsync(Player.UserId) or 0
    ds:SetAsync(Player.UserId, Cash.Value)
    ds:SetAsync(Player.UserId, Ore.Value)
    Cash.Changed:Connect(function()
        ds:SetAsync(Player.UserId, Cash.Value)
    Ore.Changed:Connect(function()
        ds:SetAsync(Player.UserId, Ore.Value)
        end)
    end)
end)

Players.PlayerRemoving:Connect(function(Player)
    ds:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
    ds:SetAsync(Player.UserId, Player.leaderstats.Ore.Value)
end)

Answer this question