How to save walkspeed and jump height upon leaving and rejoining

I’m basically trying to make it so that when the player rejoins their last walkspeed they had originally had before leaving is set upon joining but I don’t know how to override this

local humanoid = char:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(char)
local physicalStats = player:WaitForChild("PhysicalStats")
local speed = physicalStats:WaitForChild("Speed")
local speedMulti = player:WaitForChild("Multipliers"):WaitForChild("SpeedMulti")
local powerMultiplier = player:WaitForChild("Stats"):WaitForChild("PowerMultiplier")
local geneticsPowerMulti = player:WaitForChild("Stats"):WaitForChild("GeneticsPowerMulti")
local doublePowerMulti = player:WaitForChild("Stats"):WaitForChild("DoublePowerMulti")
local boostDoublePowerMulti = player:WaitForChild("Stats"):WaitForChild("BoostdoublePowerMulti")

local minWalkSpeed = 16
local walkSpeedCap = 230
local minJumpHeight = 7.2
local maxJumpHeight = 175

local lastWalkSpeed = player:FindFirstChild("LastWalkSpeed") or Instance.new("IntValue")
lastWalkSpeed.Name = "LastWalkSpeed"
lastWalkSpeed.Value = minWalkSpeed
lastWalkSpeed.Parent = player

local lastJumpHeight = player:FindFirstChild("LastJumpHeight") or Instance.new("IntValue")
lastJumpHeight.Name = "LastJumpHeight"
lastJumpHeight.Value = minJumpHeight
lastJumpHeight.Parent = player

local function calculateMaxWalkSpeed(speedValue)
	if speedValue >= 5000000 then
		return walkSpeedCap
	elseif speedValue >= 2500000 then
		return 200
	elseif speedValue >= 1000000 then
		return 175
	elseif speedValue >= 250000 then
		return 130
	elseif speedValue >= 100000 then
		return 100
	elseif speedValue >= 50000 then
		return 75
	elseif speedValue >= 20000 then
		return 50
	elseif speedValue >= 5000 then
		return 40
	elseif speedValue >= 1000 then
		return 30
	elseif speedValue >= 500 then
		return 25
	elseif speedValue >= 100 then
		return 20
	else
		return minWalkSpeed
	end
end

local function calculateMaxJumpHeight(speedValue)
	if speedValue >= 5000000 then
		return maxJumpHeight
	elseif speedValue >= 2500000 then
		return 150
	elseif speedValue >= 1000000 then
		return 100
	elseif speedValue >= 250000 then
		return 75
	elseif speedValue >= 100000 then
		return 50
	elseif speedValue >= 50000 then
		return 35
	elseif speedValue >= 20000 then
		return 25
	elseif speedValue >= 5000 then
		return 20
	elseif speedValue >= 1000 then
		return 15
	elseif speedValue >= 500 then
		return 12
	elseif speedValue >= 100 then
		return 10
	else
		return minJumpHeight
	end
end

game.ReplicatedStorage:WaitForChild("SetWalkSpeed").OnServerEvent:Connect(function(ply, speedValue)
	if ply ~= player then return end
	local speedValueNum = tonumber(speed.Value) or 0
	local maxAllowedSpeed = calculateMaxWalkSpeed(speedValueNum)
	if speedValue <= maxAllowedSpeed then
		lastWalkSpeed.Value = speedValue
		humanoid.WalkSpeed = speedValue
	else
		warn("Invalid WalkSpeed entered: ", speedValue)
	end
end)

game.ReplicatedStorage:WaitForChild("SetJumpHeight").OnServerEvent:Connect(function(ply, jumpValue)
	if ply ~= player then return end
	local speedValueNum = tonumber(speed.Value) or 0
	local maxAllowedJump = calculateMaxJumpHeight(speedValueNum)
	if jumpValue <= maxAllowedJump then
		lastJumpHeight.Value = jumpValue
		humanoid.JumpHeight = jumpValue
	else
		warn("Invalid JumpHeight entered: ", jumpValue)
	end
end)

player.CharacterAdded:Connect(function(newCharacter)
	local newHumanoid = newCharacter:WaitForChild("Humanoid")
	newHumanoid.WalkSpeed = lastWalkSpeed.Value
	newHumanoid.JumpHeight = lastJumpHeight.Value
end)

while true do
	if humanoid.MoveDirection.Magnitude > 0 or humanoid:GetState() == Enum.HumanoidStateType.Jumping then
		local speedValue = tonumber(speed.Value) or 0
		local speedMultiplier = tonumber(speedMulti.Value) or 1
		local powerMultiplierValue = tonumber(powerMultiplier.Value) or 1
		local geneticsPowerMultiValue = tonumber(geneticsPowerMulti.Value) or 1
		local doublePowerMultiValue = tonumber(doublePowerMulti.Value) or 1
		local boostDoublePowerMultiValue = tonumber(boostDoublePowerMulti.Value) or 1

		speedValue = speedValue + (1 * speedMultiplier * powerMultiplierValue * geneticsPowerMultiValue * doublePowerMultiValue * boostDoublePowerMultiValue)
		speed.Value = tostring(speedValue)
	end
	humanoid.WalkSpeed = lastWalkSpeed.Value
	humanoid.JumpHeight = lastJumpHeight.Value
	wait(1)
end
1 Like

You need to use DataStoreService, there are some advanced things with buffers and classes you can do but the simplest form is just this: (Ensure DataStoreService is enabled in game’s settings)

-- Services --

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

-- Variables --

local Table = {"hi", 54, "randomvalue"}

-- Datastore --

local Store = DataStoreService:GetDataStore("Data")

-- Runtime --

Players.PlayerAdded:Connect(function(plr)

local warning, Data = pcall(function(
return Store:GetAsync(plr.UserId.."_DATA")
end)

if Data then

print(Data)

end

end)

Players.PlayerAdded:Connect(function(plr)

local success, output = pcall(function(
return Store:SetAsync(plr.UserId.."_DATA", Table)
end)

if not success then warn(output) end

end)

Here is a documented tutorial on DataStoreService

Thanks I know that though I just don’t know how to save the walkspeed upon leaving and then add when rejoining because whenever I do it seems to go to the walkspeed for like a second then goes back to 16.