Help with Location Datastore

Hello, my name is Light!

Today I attempted to learn Datastores for my game, but no matter what I do I can’t seem to get my script to work. What really confuses me is that the print statements do exactly what they are supposed to, that is they print the players position when it is saved and read, which makes me think that this is some sort of bug but I doubt it is.

I have tested it on Studio and InGame
This can be tested here: Feature Testing - Roblox

Any help is appreciated!

local DSS = game:GetService("DataStoreService")

--Datastores
local locationStoreX = DSS:GetDataStore("PlayerLocationX")
local locationStoreY = DSS:GetDataStore("PlayerLocationY")
local locationStoreZ = DSS:GetDataStore("PlayerLocationZ")

--extra needed variables
local playerX, playerY, playerZ

game.Players.PlayerAdded:Connect(function(player)
	
	local Position = workspace:WaitForChild(player.Name).HumanoidRootPart.Position
	
	local success = pcall(function()
		playerX = locationStoreX:GetAsync(player.UserId)
		playerY = locationStoreY:GetAsync(player.UserId)
		playerZ = locationStoreZ:GetAsync(player.UserId)
	end)

	if success then
		if playerX ~= nil then
			if playerY ~= nil then
				if playerZ ~= nil then
					Position = Vector3.new(playerX, playerY, playerZ)
					print(playerX, playerY, playerZ)
				end
			end
		end
	else print("There was an issue loading player data.")
	end
	
	player.CharacterRemoving:Connect(function(character)
		
		local Position = character.HumanoidRootPart.Position
		
		local success2, err = pcall(function()
			locationStoreX:SetAsync(player.UserId, Position.X)
			locationStoreY:SetAsync(player.UserId, Position.Y)
			locationStoreZ:SetAsync(player.UserId, Position.Z)
		end)

		if success2 then
			print(Position.X, Position.Y, Position.Z)
		else print("There was an issue saving player data.")
		end

	end)
end)

Have a good day :slight_smile:

1 Like

The key to the datastore must be a string or numbers, use the player’s UserId instead of the player instance. The instance value is erased once the player leaves the game, thus compromising the key to the data stored.

Do what 8_8io said and check if you have enabled Studio Access to API Services .

This did not fix it, although I will update the code to include it. also Studio Access to API services is on.

The position variable you have indexed only provides itself as a vector3 value. To move the character to the position you must call the humanoid root part alongside its positional value.

HumanoidRootPart.Position = Position

I don’t understand, didn’t I do that at

Position = Vector3.new(playerX, playerY, playerZ)

You can’t set Properties like this sadly. You referenced Position when you set

local Position = workspace:WaitForChild(player.Name).HumanoidRootPart.Position

But this is setting Position to their current Position, not a reference to the property. To fix this, you simply remove the .Position and then change your code down below

Position = Vector3.new(playerX, playerY, playerZ)

to

Position.Position = Vector3.new(playerX, playerY, playerZ) --Would recommend renaming the variable to HumanoidRootPart or something along those lines instead of Position.

TL;DR;

You can’t make a variable a reference to an Instance Property, that will just grab the current value. To change Properties you have to reference the actual Instance.

1 Like

dang, that makes sence when you put it like that.

Phew

I was hoping I explained it and it made sense, I at one point was like “Does this even make sense?” xD