Saving Player Position

Hello, I am practicing on my datastore script, basically how do I save the players position, this is my script so far, I have no idea how to basically script something in datastores.

local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local PlayerPosition = Character.HumanoidRootPart.Position
		local PlayerData 
		local success, errormessage = pcall(function()
			local PlayerData = MyFirstDataStore:GetAsync(player.UserId)
		end)

		if success then
			PlayerPosition = PlayerData
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	player.CharacterRemoving:Connect(function(Character)
		local PlayerData = Character.HumanoidRootPart.Position
		local success, errormessage = pcall(function()
			MyFirstDataStore:SetAsync(player.UserId, PlayerData)
		end)
		if success then
			print("success")
		else
			print("errror")
		end
	end)
end)

Alot of people will say data stores can only store tables. This isn’t neccesarily true but it can be reasonable to use tables knowing that they can only have 1 data thingy for each data store. Either way you need to get PlayerData before the character is removing because if the player is removing the player will already be gone (along with its character data) in short, simply get rid of that character removing thing and have Character equal to player.Character

How in the world do I do that, haha

on this forum i try and teach instead of spoonfeed but here is your fixed script

local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local PlayerPosition = Character.HumanoidRootPart.Position
		local PlayerData 
		local success, errormessage = pcall(function()
			local PlayerData = MyFirstDataStore:GetAsync(player.UserId)
		end)

		if success then
			PlayerPosition = PlayerData
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
		local PlayerData = player.Character.HumanoidRootPart.Position
		local success, errormessage = pcall(function()
			MyFirstDataStore:SetAsync(player.UserId, PlayerData)
		end)
		if success then
			print("success")
		else
			print("errror")
		end
end)
3 Likes

Thanks, I will have my scripting instructor teach me what I did wrong, she was just busy for now so I came on here, thanks, your answer is now marked as solution.

Wait, if you are doing it in studio u need to call bind to close else it wont work

That script won’t work at all, you can’t save a data type.

You can silent, in fact you can see on the documentation for the SetAsync() thing that they actually use the number data type

I don’t think it works when you try to save a vector3 but if it works then, i’ll rather use this way anyway.

local DataStoreService = game:GetService("DataStoreService")
local MyFirstDataStore = DataStoreService:GetDataStore("MyFirstDataStore")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local PlayerPosition = Character.HumanoidRootPart.Position
		local PlayerData = nil 
		local success, errormessage = pcall(function()
			local PlayerData = MyFirstDataStore:GetAsync(player.UserId)
		end)

		if success then
			PlayerPosition = PlayerData
			local rootpart = Character:WaitForChild("HumanoidRootPart")
			
			rootpart.Position = Vector3.new(PlayerPosition.X,PlayerPosition.Y,PlayerPosition.Z)
			
			
		end
	end)
end)


game.Players.PlayerRemoving:Connect(function(player)

	local PlayerData = player.Character.HumanoidRootPart.Position
	
	local success, errormessage = pcall(function()

		MyFirstDataStore:SetAsync(player.UserId,{
			x = PlayerData.X,
			y = PlayerData.Y,
			z = PlayerData.Z 
		})

	end)

	if success then
		print("success")
	else
		print("errror")
	end

end)

I recommend you to also save the lookvector, which is possible.

I don’t know why you’re boldly providing false information, what I mean by data type is the common unsaveable data types such as Vectors, CFrames and etc, I’m not talking about numbers. Test this out your self.

I am just stating what I see on the documentation. I havent tested it but it says you can save data types and they use it in their code examples

It doesn’t say you can save data types, you cannot save a CFrame/Vector data type. You want to serialize them instead into a table in order to save them.

I don’t know why you’re arguing pointlessly, test this out your self. You cannot save a Vector/CFrame data type, the documentation never states you can save them. Please stop misspreading information.

But It doesn’t change the fact you can’t save a CFrame/Vector data type though.

1 Like

Yep, just tested it out. Turns out it cant save classes like @SilentsReplacement suggested. If thats the case just do what I said earlier with returning the vector3 then save its components as we can save the number values

1 Like