Sprinting Mechanic is Applied to the Wrong Team

Hey everyone,

The sprinting system in my game is intended to be applied to one team but applies to another team (note that this is only one player) that should not have it enabled. The implication is that, after pressing Left Shift, the latter team has their WalkSpeed permanently decreased from 18 to 16.

The only team that should be affected by the sprinting system are the “Agents”. Despite other teams not having this mechanic, players may opt to or inadvertently press Left Shift, ruining the player’s movement for the duration of the round.

Script for Critique (Local):

local Player = game.Players.LocalPlayer
local Character = Player.Character

local ContextActionService = game:GetService("ContextActionService")

local Stamina = 100
local RunRefresh = 20 -- when to allow running after exhausted

local Running = false
local Exhausted = false

local function Sprint(_, state)
	if state == Enum.UserInputState.Begin then
		if Player.Team.Name == "Agents" then
			if Exhausted then return end
			Running = true
			Player.Character.Humanoid.WalkSpeed = 28

			while Running do
				if Stamina > 0 then
					Stamina -= 0.25
					script.Parent:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
				else
					Exhausted = true
					Player.Character.Humanoid.WalkSpeed = 16
					break
				end

				task.wait()
			end
		end
	elseif state == Enum.UserInputState.End then
		Running = false
		Player.Character.Humanoid.WalkSpeed = 16
	end
end

task.spawn(function() -- Not yield code
	while true do
		if Stamina < 100 and not Running then
			Stamina += 0.075
			script.Parent:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			task.wait() -- Delay between stamina gain
			Exhaustion()
		end
		task.wait()
	end
end)

function Exhaustion()
	if Stamina > RunRefresh then -- Player may run again
		Exhausted = false
	end
end

ContextActionService:BindAction("Sprint", Sprint, true, Enum.KeyCode.LeftShift)
ContextActionService:SetPosition("Sprint", UDim2.new(0.5, 0, -0.5, 0))
ContextActionService:SetTitle("Sprint", "Sprint")

In the Sprint() function on the elseif state == Enum.UserInputState.End then line, shouldn’t you also be checking there for whether the player is on the Agents team? That’s probably what’s causing the opposite team to have their speed reset to 16.

1 Like

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