Sorry, but using too many functions is really a bad idea @regexman
And in my opinion what @ElVaranCubico_YT said was correct
Below is an example, why functions for saving is hard
Example 1 (Functions):
local function getmaterial(instance)
local stringtoreturn = tostring(instance.Material)
return stringtoreturn
end
local table = {
["Material"] = getmaterial(instance)
}
Both the methods work, but a 1 line script is better than a 4 line script
Example 2 (Normal):
local table = {
["Material"] = tostring(instance.Material)
}
See how easy it is
I’m not telling what you explained was wrong, but I’m just suggesting you to mention an easy way
How exactly would I use this to save the player’s position? I’ve attempted it with this method but I’ve obviously done it wrong.
Here is my code:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Position")
game.Players.PlayerAdded:Connect(function(plr)
local Character = plr.Character or plr.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
Position = {X = Root.CFrame.X, Y = Root.CFrame.Y, Z = Root.CFrame.Z}
local RetreveData = DataStore:GetAsync("plr") or nil
if RetreveData ~= nil then
Root.CFrame = CFrame.new(RetreveData.X, RetreveData.Y, RetreveData.Z)
end
end)
game.Players.PlayerRemoving:Connect(function()
DataStore:SetAsync("plr", Position)
end)
I’m not really good at datastore but… Aren’t you doing GetAsync(“plr”)? For what I’ve read on an article about it, you have to put the player.UserId, and you are just getting the data from the key “plr” (UserId is a key).
Try replacing "plr" with plr, actually, use plr.UserId
okay, ill send you my old script that works, heres the script
local position = game:GetService("DataStoreService"):GetDataStore("PlayerPositions")
game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
char:WaitForChild("Humanoid")
char:WaitForChild("HumanoidRootPart")
-- load position if data exists
local retrieveddata = position:GetAsync(plr.UserId) or nil
if retrieveddata ~= nil then
char:SetPrimaryPartCFrame(CFrame.new(Vector3.new(table.unpack(retrieveddata))))
print("Set primary part cframe")
end
task.wait(2)
while true do
print("Saving data")
position:SetAsync(plr.UserId,{char.HumanoidRootPart.Position.X,char.HumanoidRootPart.Position.Y,char.HumanoidRootPart.Position.Z})
task.wait(10)
end
end)
Wait. I’m so sorry. It’s working now. Apparently I forgot to let it save before pressing the stop button I’m such an idiot. I’m so sorry. Should I make it so that it saves when the player leaves? I’m thinking maybe that would be better.