Variables not changing value

Greetings,

I’m trying to make a sprint script with a skill system to which you add points to increase the stamina and speed. But when the value is changed, it doesn’t do anything. It stays the same. How can I fix this?

local maximumstamina = script.Stamina.Value --stamina
local speed = script.Speed.Value --running speed
local normalspeed = 11 --normal speed
local staminadrain = 1 --stamina drain per second
local staminaregen = script.StaminaRegen.Value --stamina regeneration per second

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
local stamina = MAX_STAMINA
local isRunning = false

local function updateStamina(deltaTime)
	if isRunning then
		stamina = math.max(stamina - staminadrain * deltaTime, 0)
	else
		stamina = math.min(stamina + staminaregen * deltaTime, maximumstamina)
	end
end

local function updateSpeed()
	if isRunning and stamina > 0 then
		humanoid.WalkSpeed = speed
	else
		humanoid.WalkSpeed = normalspeed
	end
end

userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
	end
end)

while true do
	local deltaTime = wait(1/60)
	updateStamina(deltaTime)
	updateSpeed()
end

This is an indirectional change of values. consider taking the values per updating

local maximumstamina = script.Stamina --stamina
local speed = script.Speed --running speed
local normalspeed = 11 --normal speed
local staminadrain = 1 --stamina drain per second
local staminaregen = script.StaminaRegen--stamina regeneration per second

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
local stamina = MAX_STAMINA
local isRunning = false

local function updateStamina(deltaTime)
	if isRunning then
		stamina = math.max(stamina - staminadrain * deltaTime, 0)
	else
		stamina = math.min(stamina + staminaregen.Value * deltaTime, maximumstamina.Value)
	end
end

local function updateSpeed()
	if isRunning and stamina > 0 then
		humanoid.WalkSpeed = speed.Value
	else
		humanoid.WalkSpeed = normalspeed
	end
end

userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
	end
end)

while true do
	local deltaTime = wait(1/60)
	updateStamina(deltaTime)
	updateSpeed()
end

Thanks a lot for the help, Next time I will know.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.