I am trying to make a datastore for my game and I dont think it works, it removes my head
The game is about growing my head but it doesnt save, thats what Im trying to achieve
Datastore Script
local dss = game:GetService("DataStoreService")
local mdss = dss:GetDataStore("HeadGrowthSaver")
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid
while not humanoid do
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
character.ChildAdded:Wait()
end
end
local Head = humanoid.HeadScale
local data
local success, errormessage = pcall(function()
data = mdss:GetAsync(player.UserId.."-Head")
end)
if success then
Head.Value = data
else
print("Error with saving Your Head")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
mdss:SetAsync(player.UserId.."-Head",player.Character.Humanoid.HeadScale)
end)
if success then
print("Saved Head")
else
print("Error with saving Head")
warn(errormessage)
end
end)
Anyway the issue is that the player’s chatacter is removed before the PlayerRemoving event is fired, so I assume that you are getting a reference error, probably that player.Character is nil, is that case? If that’s the case then obviosuly it won’t be able to save the HeadScale, Moreover HeadScale is an instance and instances cannot be saved in DataStores, so you need to save HeadScale.Value.
Using SetAsync() every time is bad practice and it should only be used when saving the data for the first time, if the data was already saved once you should use UpdateAsync() instead.
--HeadGrowth
local HeadSize = 10000000000000000000
local SpeedOfScaling = 0.0003
local I = 1
local X = 0
local function check(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Humanoid.HeadScale.Value == 1 then
if X == 0 then
X = 1
I = 1
while I <= HeadSize do
wait(0.01)
hit.Parent.Humanoid.HeadScale.Value = I
I = I+SpeedOfScaling
end
while X == 1 do
X = 0
wait(0.01)
end
end
end
end
end
script.Parent.Touched:connect(check)
One problem is that the success variable will always be true unless the :GetAsync() itself errors.
if success then
if data then
Head.Value = data
else
print("No data")
end
else
print("Error with saving Your Head")
warn(errormessage)
end
Another problem would be that you are testing this in studio. Studio typically closes the game before data can save.
local dss = game:GetService("DataStoreService")
local mdss = dss:GetDataStore("HeadGrowthSaver")
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid
while not humanoid do
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
character.ChildAdded:Wait()
end
end
local Head = humanoid.HeadScale
local data
local success, errormessage = pcall(function()
data = mdss:GetAsync(player.UserId.."-Head")
end)
if success then
if data then
Head.Value = data
else
print("No data")
end
else
print("Error with saving Your Head")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
mdss:SetAsync(player.UserId.."-Head",player.Character.Humanoid.HeadScale.Value)
end)
if success then
print("Saved Head")
else
print("Error with saving Head")
warn(errormessage)
end
end)
game:BindToClose(function()
for i, player in pairs(game.Player:GetPlayers()) do
local success, errormessage = pcall(function()
mdss:SetAsync(player.UserId.."-Head",player.Character.Humanoid.HeadScale.Value)
end)
if success then
print("Saved Head")
else
print("Error with saving Head")
warn(errormessage)
end
end
end)
Also, you tried to save the HeadScale itself, and not the HeadScale Value (just realized this last problem was already mentioned).
Use Profile Service. Although this probably isn’t related to your issue, but Profile Service stops all kinds of data loss. If someone rejoins servers super fast, they might lose data. Or when games get so much traffic, they’ll get data loss too.
Also,
this is very inefficient. Just do a player.CharacterAdded:Connect(function(character) and grab their humanoid with a :WaitForChild() from there.