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

How do I save a leaderstat with a space in its name in a Datastore?

Asked by 5 years ago
Edited 5 years ago

So I have a datastore and leaderboard script with three different stats in it and one of them has a space in it, how would I save that? Because with what I have, whenever I join in the game, edit the stats, then leave and rejoin the stats don't save, so maybe its the third stat?

local DS = game:GetService("DataStoreService")

game.Players.PlayerAdded:connect(function(plr)
    local ls = Instance.new("Folder",plr)
    ls.Name = "leaderstats"
    local cash = Instance.new("NumberValue", ls)
    cash.Name = "Cash"
    local rank = Instance.new("StringValue", ls)
    rank.Name = "Rank"
    local blocksm = Instance.new("NumberValue", ls)
    blocksm.Name = "Blocks Mined"
    local id = plr.userId
    cash.Value = DS:GetDataStore("cash"):GetAsync(id) or 0
    rank.Value = DS:GetDataStore("ranks"):GetAsync(id) or "Beginner Miner"
    blocksm.Value = DS:GetDataStore("blocksm"):GetAsync(id) or 0
end)
game.Players.PlayerRemoving:connect(function(plr)
    local id = plr.userId
    local ls = plr:FindFirstChild("leaderstats")
    if ls and id then
        DS:GetDataStore("cash"):SetAsync(id, ls.Cash.Value)
        DS:GetDataStore("ranks"):SetAsync(id, ls.Rank.Value)
        DS:GetDataStore("blocksm"):SetAsync(id, ls.BlocksMined.Value)
    end
end)

My game is FE.

0
Please use code blocks in your post so I can read it. T0XN 276 — 5y
0
sorry, fixed it K1ng_Phant0m 13 — 5y
0
Use :FindFirstChild("Blocks Mined") TiredMelon 405 — 5y
0
Object["Child Name] or Object:FindFirstChild("Child Name") or even Object:WaitForChild("Child Name") EpicMetatableMoment 1444 — 5y
0
ok thanks K1ng_Phant0m 13 — 5y

1 answer

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

There are two methods for gettings an objects child if the child's name has a space in it.

Method 1:

Parent:FindFirstChild("Child With Spaces")

Method 2:

Parent["Child With Spaces"]

With this, this:

DS:GetDataStore("child with space"):SetAsync(id, ls.child with space.Value)

would correctly be:

DS:GetDataStore("child with space"):SetAsync(id, ls["child with space"].Value)

or

DS:GetDataStore("child with space"):SetAsync(id, ls:FindFirstChild("child with space").Value)

P.S. Make sure to also save on game.BindToClose

Ad

Answer this question