[HELP] How can I fix this script

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

Kaiden’s script doesn’t account for the fact that the player’s chatacter is removed before PlayerRemoving is fired.

1 Like

Ok I tested the script and it doesnt save my head size, whenever I rejoin, it resets my head to its original size

I duplicated the line here (local character) from the other part of the script, did I do it right?

game.Players.PlayerRemoving:Connect(function(player)
	
	local character = player.Character or player.CharacterRemoving:Wait()
	
	local success, errormessage = pcall(function()
		mdss:SetAsync(player.UserId.."-Head",player.Character.Humanoid.HeadScale.Value)
	end)

As i said, by the time that PlayerRemoving fires the character already doesn’t exist anymore. You will need to attach a CharacterRemoving event when the player joins but since that event also fires when they die you will need to check whether or not their chatacter is removed because they are leaving or because they died