Changing Value Help

i made a running script and i use int value for walkspeed. it seems fine it works but when int value change the player walkspeed will be still the same as the origin int value and i cant find any solution how it will change the walkspeed when the int value change.

here is the script:

local player = game.Players.LocalPlayer.Character
local Pressed = game:GetService("UserInputService")
local debounce = 1
local Run = player.Humanoid:LoadAnimation(script.RunAnim)
local TweenService = game:GetService("TweenService")
local runvalue = player.RunSpeed.Value
local walkvalue = player.NormalSpeed.Value
Running = false

Pressed.InputBegan:Connect(function(key,speed)
	if key.KeyCode == Enum.KeyCode.LeftControl and debounce == 1 and player.Stunned.Value == false then
		player.Humanoid.WalkSpeed = runvalue
		Running = true
		debounce = 2
	elseif key.KeyCode == Enum.KeyCode.LeftControl and debounce == 2 and player.Stunned.Value == false then
		player.Humanoid.WalkSpeed = walkvalue
		Running = false
		debounce = 1
		if Run.IsPlaying then
			Run:Stop()
		end
	end
end)

player.Humanoid.Running:Connect(function(speed)
	if speed >= 10 and Running == true and not Run.IsPlaying and player.Stunned.Value == false then
		Run:Play()
	elseif speed >= 10 and Running == false and Run.IsPlaying and player.Stunned.Value == false then
		Run:Stop()
	elseif speed < 10 and Run.IsPlaying and player.Stunned.Value == false then
		Run:Stop()
	end
end)

player.Humanoid.Changed:Connect(function()
	if player.Humanoid.Jump and Run.IsPlaying then
		Run:Stop()
	end
end)

im new to scripting

You are only setting runvalue and walkvalue at the very start of the script, hence why it isn’t updating.

If you append the following code to your script, it should make it work with your intended behaviour:

player.RunSpeed.Changed:Connect(function()
    runvalue = player.RunSpeed.Value
    if Running then
        player.Humanoid.WalkSpeed = runvalue
    end
end)

player.NormalSpeed.Changed:Connect(function()
    walkvalue = player.NormalSpeed.Value
    if not Running then
        player.Humanoid.WalkSpeed = walkvalue
    end
end)

It basically checks for either of the values to change, then updates the variable in the script. The if statement check if the player is currently running / not running.

If they are running and the value that got updated was the running value, then it changes the player’s WalkSpeed. If they are walking and the value that got updated was the walking value, then again it changes the player’s WalkSpeed.

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