Scripts with spaces

Hi. I recently made a datastore system for a leaderstat. However it does not work as when I type the leaderstat in the script it breaks, as the leaderstat’s name is 2 words with a space. Is there a way I can write it in a script while still having the leaderboard show as 2 words?

A quick example:
Imagine I have a leaderstat called Crystal Shards. I want the leaderboard at the top to say Crystal Shards. The intValue is therefore named Crystal Shards. However when I write a script like so;

X = game.Players.RealPlayerName.leaderstats.Crystal Shards.Value

It fails as it cannot find leaderstats.Crystal, and sets the value to nil

Any help would be appreciated

here’s an example of referencing something with a space
workspace["Some Folder of Parts"]["The First Part"].Name

you could also still use FindFirstChild and WaitForChild

I tried using
leaderstats[“intValue Name”].Value

got the error; “expected identifier, got [”

for reference, here is the entire line of code:

deathsDatastore:SetAsync(player.UserId…"-deaths",player.leaderstats[“Variable Name”].Value)

(Now with the recommended leaderstats[""])

it looks like it should work
are you maybe wrapping the variable name with quotes by accident?

like if you’re trying to get “Play Time” from a player, and it’s saved under the variable Stat, you’d do player[Stat], not player["Stat"]

oh haha yea lemme test it with that

Edit: nope, both words become underlined in red

1 Like

Could you show your entire code?

local dataStoreService = game:GetService("DataStoreService")
local deathsDatastore = dataStoreService:GetDataStore("deathsDatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local deaths = Instance.new("IntValue")
	deaths.Name = "skill issues"
	deaths.Parent = leaderstats

	player.CharacterAdded:Connect(function()
		repeat wait() until player.Character ~= nil
		local character = player.Character
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()
			deaths.Value += 1
		end)
	end)
	
	local data
	
	local success, errormessage = pcall(function()
		data = deathsDatastore:GetAsync(player.UserId.."-deaths")
	end)
	
	if success then
		deaths.Value = data
		print("Player deaths datastore successfully retrieved")
	else
		print("Player deaths datastore retrieval failed")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		deathsDatastore:SetAsync(player.UserId.."-deaths",player.leaderstats["skill issues"].Value)
	end)
	
	if success == true then
		print("Player deaths leaderstat saving successful")
	else
		print("Player deaths leaderstat saving failed")
		warn(errormessage)
	end
end)

The console says that the datastore was successfully retrieved, but the leaderstat stays at 0. Can’t see the aftereffects of leaving because then the console gets deleted

you could test in studio with a local server
go into the Test tab, and click Start Server with 1 player

hmm, trying to enable studio api services isnt working. Ill try later and get back to you

looks like you arent parenting the leaderstats to the player
nvm, it’s in Instance.new

If you’re testing in studio you may need to use Game:BindToClose.
https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose