Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

5 comments

0

Please use code blocks in your post so I can read it.

0

sorry, fixed it

0

Use :FindFirstChild("Blocks Mined")

0

Object["Child Name] or Object:FindFirstChild("Child Name") or even Object:WaitForChild("Child Name")

0

ok thanks

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.