[HELP] How can I fix this script

Hello developers, again

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)

Thanks for reading

1 Like

your code seems fine. can u state the problem with the code

You didn’t tell us if the script is erroring.

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.

add .Value to the end and the code will probaly work

This is an extremely common problem among beginner scripters. The answer is in this post: How to properly save player data in data stores upon server close

wait hold up. headscale is not a property of humanoid unless you create that with instance.new

HeadScale is a default NumberValue inside the Humanoid and is used to regulate a player’s Head size

ok didnt know that

This text will be blurred

It removes my head and head accessories

Ok, maybe this might help

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

Try and see, if there are errors… Just let us know :smiley:

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

Ok good to know, i will try this tomorrow

Its like 9:00 rn

Thanks

That was the head growth script, not the headsave script

It works perfectly nonetheless

It doesnt error, it just removes my head instead of saving the size

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.

By “it just removes my head” you mean that the DataStore doesn’t save the increased value of HeadScale, right?

Have you corrected the errors that I highlighted in my previous post?

Yeah, basically, because I look like Im wearing the headleass head, my head is gone
But I Dont know if it saves

Did you correct the errors i pointed out? You were saving the HeadScale instance and not the value

No I didnt, I did not have the time, but I can try to now

Also, Im using kaiden’s script, and my head is visible! yay, but it doesnt save

it prints No Data all the time