Consistently check values in numbervalue

So the title is quite self explanatory, i need it to consistently check the value so it could also change the speed. I have tried .Changed but it just breaks my code, and i really dont know where to place it so i really need your help.

The Code:


local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

--These are the values to be consistently checked

local maxSpeed = Character.RunningScript.Speed.Value --self explanatory 
local acceleration = Character.RunningScript.Acceleration.Value --its kinda self explanatory also 


local currentSpeed = 18

local currentAnimSpeed = 0.4
local isAccelerating = false
local isRunning = false

local runSound = game.SoundService.Running
runSound.PlaybackSpeed = currentSpeed / maxSpeed 

local function increaseSpeed()
	if currentSpeed < maxSpeed then
		currentSpeed = currentSpeed + acceleration
		currentAnimSpeed = currentAnimSpeed + (acceleration / maxSpeed)
	else
		currentSpeed = maxSpeed
		currentAnimSpeed = 1.1
		isAccelerating = false
	end

	Character.Humanoid.WalkSpeed = currentSpeed
	PlayAnim:AdjustSpeed(currentAnimSpeed)
	runSound.PlaybackSpeed = currentSpeed / maxSpeed 
end
function ToggleAnimationAndSound(shouldPlay)
	if shouldPlay then
		if not PlayAnim.IsPlaying then
			PlayAnim:Play()
		end
		if not runSound.IsPlaying then
			runSound:Play()
		end
	else
		PlayAnim:Stop()
		runSound:Stop()
	end
end

local leftControlDown = false

local heartbeatflag = false
game:GetService('RunService').Heartbeat:Connect(function()
	ToggleRunningIfAirborne()
	if isAccelerating then
		increaseSpeed()
	end
	if Character.Humanoid.MoveDirection ~= Vector3.new() then
		if heartbeatflag then return end
		heartbeatflag = true
		isRunning = true
		isAccelerating = true
		local Anim = script.SprintAnim
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		ToggleAnimationAndSound(true)
		print('Running')
	else
		if not heartbeatflag then return end
		heartbeatflag = false
		Character.Humanoid.WalkSpeed = 13

		ToggleAnimationAndSound(false)
		currentSpeed = 13
		currentAnimSpeed = 0.4
		isAccelerating = false
		isRunning = false
		print('Running stopped')
	end
end)

function ToggleRunningIfAirborne()
	if Character.Humanoid.FloorMaterial == Enum.Material.Air or leftControlDown then
		ToggleAnimationAndSound(false)
	elseif isRunning then
		ToggleAnimationAndSound(true)
	end
end

game:GetService('RunService').Heartbeat:Connect(function()
	ToggleRunningIfAirborne()
	if isAccelerating then
		increaseSpeed()
	end
end)

Try to use the .Changed event to add listeners for maxSpeed and acceleration so the values update automatically when they change. Create an update function that adjusts the character’s speed and sound playback whenever maxSpeed is modified. Also, make sure that currentSpeed does not exceed maxSpeed if it changes. Optionally, you can implement logic to handle changes in acceleration as needed. This should help you fix it lol.

1 Like

Would you mind giving me a sample because each time when i do it, it either will break the script or just doesnt work when changed

and im a beginner so yea im still learning

Im still stuck with this thing

Try using Instance | Documentation - Roblox Creator Hub

So write down

numberValue:GetPropertyChangedSignal("Value"):Connect(function()
    -- your code
end)
1 Like

Thanks, Luke! This helped me alot :slight_smile:

1 Like

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