Operators aren't subtracting, and i'm not understanding why

So, i’m trying to make a sprint, and i’m using a method where it essentially counts how much the button is pressed, then resets it at a certain point. Now, for some reason whenever it resets, the walkspeed doesn’t go down. Please help me out with this!

local gui = script.Parent
local uis = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")

local activateCount = 0

local sprintVal = 8

uis.InputBegan:Connect(function(inpObj, gpe)
	if gpe == true then return end

	if gui.Enabled == true then
		if inpObj.KeyCode == Enum.KeyCode.R then
			activateCount += 1
			humanoid.WalkSpeed += sprintVal
			print(humanoid.WalkSpeed)
			print(activateCount)
		end
		
		if activateCount == 2 then
			humanoid.WalkSpeed -= sprintVal
			activateCount = 0
			print(humanoid.WalkSpeed)
			print(activateCount)
		end
	end
end)

I found the issue.

Basically when you press R it’s increasing the walkspeed then it checks if the activate count is 2 which just reverts the addition instead use this script which doubles the sprint value when subtracting

local gui = script.Parent
local uis = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")

local activateCount = 0

local sprintVal = 8

uis.InputBegan:Connect(function(inpObj, gpe)
	if gpe == true then return end

	if gui.Enabled == true then
		if inpObj.KeyCode == Enum.KeyCode.R then
			activateCount += 1
			humanoid.WalkSpeed += sprintVal
			print(humanoid.WalkSpeed)
			print(activateCount)
		end

		if activateCount == 2 then
			humanoid.WalkSpeed -= sprintVal * 2
			activateCount = 0
			print(humanoid.WalkSpeed)
			print(activateCount)
		end
	end
end)
1 Like

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