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