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)

3 comments
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?
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.
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().