Why is my data "nil"?

I’m trying to store a gui’s last position and then place the gui there when the player joins again.

game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder")
	local Position = Instance.new("StringValue")
	Folder.Name = "Positions"
	Folder.Parent = player
	Position.Parent = Folder
	Position.Name = "Position"

	local data
	
	local success, errormessage = pcall(function()	
		data = MyDataStore:GetAsync(player.UserId.."-Pos")
	end)
	
	if success then
		warn(data)
		Position.Value = data
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local SetPositionValueXScale = player:WaitForChild("PlayerGUI").ScreenGui.TextLabel.Position.X.Scale
	local success, errormessage = pcall(function()	
		MyDataStore:SetAsync(player.UserId.."-Pos", SetPositionValueXScale)
	end)
	
	warn(SetPositionValueXScale)
	
	if success then
		warn("cool")
	else
		warn(errormessage)
	end
end)
1 Like

PlayerUserId is a number
whenever your combining it with another string you must put it in tostring

tostring(player.UserId)

hopes this helps

2 Likes

Where do I put this? In getasync(), setasync(), or both?

do both(this is not 31 characters so this exists DEVFORUM REQUIRES REPLYS TO BE 29 + 1CHARACTERS NOT TOSTRING)

1 Like

Thank you! Do I put the “-pos” inside the parentheses or not? (I’m bad at using string lol)

Nope
tostring turns the value inside to a string so that would just serve no point + cause another issue

1 Like

I edited the script a bit before you replied, so my error went from it being “nil” to being literally nothing. I made your edit, it’s still nothing for some reason.

Is your goal to save textlabels position?

1 Like

And also are you sure you have access to datastores? did you turn on the toggle

1 Like

Yeah (31 - 1 characters MMMMMMMMMM)

Yeah, again AAAAAAAAAAAAAAAAAAAA

roblox seriously censors 3,0 charcters

1 Like

yeah, unfortunately. (okokokokokokok)

Ok, for one,

This is false, you can combine a number with a string without using tostring.
Second, the reason it’s not saving and is returning nil is because you are testing in Studio. Studio has a habit of closing the game before you can save the data. So, you can use game:BindToClose to save the data properly.

game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder")
	local Position = Instance.new("StringValue")
	Folder.Name = "Positions"
	Folder.Parent = player
	Position.Parent = Folder
	Position.Name = "Position"

	local data
	
	local success, errormessage = pcall(function()	
		data = MyDataStore:GetAsync(player.UserId.."-Pos")
	end)
	
	if success then
		warn(data)
		Position.Value = data
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local SetPositionValueXScale = player:WaitForChild("PlayerGUI").ScreenGui.TextLabel.Position.X.Scale
	local success, errormessage = pcall(function()	
		MyDataStore:SetAsync(player.UserId.."-Pos", SetPositionValueXScale)
	end)
	
	warn(SetPositionValueXScale)
	
	if success then
		warn("cool")
	else
		warn(errormessage)
	end
end)

game:BindToClose(function()
	for i,player in pairs(game.Players:GetPlayers()) do
		local SetPositionValueXScale = player:WaitForChild("PlayerGUI").ScreenGui.TextLabel.Position.X.Scale
		local success, errormessage = pcall(function()	
			MyDataStore:SetAsync(player.UserId.."-Pos", SetPositionValueXScale)
		end)
	
		warn(SetPositionValueXScale)
	
		if success then
			warn("cool")
		else
			warn(errormessage)
		end
	end
end)
2 Likes

OH didnt know it worked with number
I remember It didn’t
thank you : )

1 Like

I’ve actually been testing in-game, not in studio. I’ll change my code to that though, cause I’m assuming it’s better.

Then I believe that the problem should be that you change the

on the client. If this is correct, you should use a remote event and send the new Scale to the server when it is changed.

1 Like

Also use updateAsync() it is better

1 Like

“104: Cannot store UDim2 in data store. Data stores can only accept valid UTF-8 characters.”

try

MyDataStore:SetAsync(player.UserId.."-Pos", tonumber(SetPositionValueXScale))
1 Like