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)
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.
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)