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 