Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

The Height wont save?

Hello so I made a randomised height script with a datastore and im having issues saving the players randomised height. I'm trying to make it so when the player leaves the game the height they had gets saved so if they rejoin they would still have that height. What am I doing wrong?

Heightsave = game:GetService("DataStoreService"):GetDataStore("HS")


game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local Heights = {2, 4, 8, 16}
	local randomHeight = Heights[math.random(1,#Heights)]
	repeat wait(.2) 
		
	until plr.Character
	
	local hum = plr.Character:WaitForChild("Humanoid")
	local Height = hum:WaitForChild("BodyHeightScale")
	
	
	local key = plr.UserId
	local savedHeight = Heightsave:GetAsync(key)
	
	
	if savedHeight then
		--loads player that already joined game height
		
		Height.Value = savedHeight[2]
		
	else
		-- player never joined so sets there height
		
		Height.Value = randomHeight
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local key = plr.UserId
	repeat wait(.2) 
		
	until plr.Character
	local hum = plr.Character:WaitForChild("Humanoid")
	local Height = hum:WaitForChild("BodyHeightScale")
	local toSaveHeight = {plr.Height.Value}
	Heightsave:SetASync(key, toSaveHeight)
end)

1 answer

Unless I'm missing something, this:

Height.Value = savedHeight[2]

and this:

local toSaveHeight = {plr.Height.Value}

don't seem compatible. You're saving an array with only one element, but trying to access an element with index 2 when you load the data.

The second issue is that here:

local Height = hum:WaitForChild("BodyHeightScale")
local toSaveHeight = {plr.Height.Value}

You have a local variable called Height, but you're trying to save plr.Height.Value, which should just be Height.Value

3 comments

0

Yeah mb I was counting the key as element aswell for the ffirst issue and the second issue i got mixed up haha. I fixed them but it seems it doesnt save still :/ Is there anything else I did wrong?

0

Have you tried using an admin command to force a save? The trouble with trying to test this is that if you're only saving on PlayerRemoving, you can't test it solo, because the server shuts down instantly when you disconnect. You either need a BindToClose() function waiting for your save to complete, or another player on the server keeping it alive while you leave and come back.

0

Because you've got Wait() and WaitForChild in your PlayerRemoving, you're almost guaranteed this code won't run for the last player to leave the server. Look into using BindToClose().

This discussion is preserved as it was. New answers, comments, and votes are closed.