Stun variable not working

So im making a medkit item for my game, that when used has 10 second timer before it heals you, and in those 10 seconds, i dont want the player to be able to sprint. So I created a leaderstat called Stun and set its value to 0, and in the medkit code it sets it to 1 whenever the medkit is used, until it is either unequipped or finishes the 10 second timer. The issue is that no matter what I do, I just cant seem to find a way to make the player stop sprinting while stun is 1. I can make him unable to sprint, but the issue is if they use the medkit WHILE sprinting. I’m trying to make it automatically make them stop sprinting. Ive tried SO MANY ways over the past like 5-6 hours, and I just cannot find the solution to this, so that brings me here as a last resort.

Here’s the code for the sprint script: (only thing that really need looked at is the functions area and below)

--{{Services}}--
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local TS = game:GetService("TweenService")

local Camera = game.Workspace.Camera
local sprintAnim = script.Sprint


--{{Tweens}}--
local sprintEaseInfo = TweenInfo.new(0.5, 
	Enum.EasingStyle.Quad, 
	Enum.EasingDirection.InOut,
	0,
	false
)

local fovEaseInfo = TweenInfo.new(1, 
	Enum.EasingStyle.Quad, 
	Enum.EasingDirection.InOut,
	0,
	false
)

-- {{Variables}} --
--Player
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playergui = player.PlayerGui
local animator = humanoid:WaitForChild("Animator")
local aniamtionTrack = humanoid:LoadAnimation(sprintAnim)

local leaderstats = player:WaitForChild("leaderstats")
local notShown = leaderstats:WaitForChild("notShown")
local Stun = notShown:WaitForChild("Stun")
--Stun = Stun.Value

--Settings
local stamina = 100
local maxStamina = 100

local playerWalkSpeed = 11
local playerSprintSpeed = 24
--local speedDifference = 8

local drainRate = 9
local refreshRate = 8.5
local staminaRefresh = 0

local walkFOV = 70
local sprintFOV = 90

local minStamina = 10

-- Booleans
local sprintHeld = false
local sprinting = false
local exhausted = false
local sprintable = true
local stunDB = true

-- {{ Functions }} --
local function sprint(active)
	if exhausted then return end
	if not sprintable then return end
	
	print("sprint function")
	if active then
		local sprintTween = TS:Create(player.Character:FindFirstChild("Humanoid"), sprintEaseInfo, {WalkSpeed = playerSprintSpeed})
		sprintTween:Play()

		local fovTween = TS:Create(Camera, fovEaseInfo, {FieldOfView = sprintFOV})
		fovTween:Play()
		
		aniamtionTrack:Play()
	elseif not active then
		local sprintTweenUndo = TS:Create(player.Character:FindFirstChild("Humanoid"), sprintEaseInfo, {WalkSpeed = playerWalkSpeed})
		sprintTweenUndo:Play()

		local fovTweenUndo = TS:Create(Camera, fovEaseInfo, {FieldOfView = walkFOV})
		fovTweenUndo:Play()
		
		aniamtionTrack:Stop()
	end
	
	--humanoid.WalkSpeed = playerSprintSpeed
	--humanoid.WalkSpeed = active and humanoid.WalkSpeed + speedDifference or humanoid.WalkSpeed - speedDifference
	sprinting = active
end

local function onInput(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and input.UserInputType ~= Enum.UserInputType.Gamepad1 and Stun.Value == 0 then -- THIS PREVENTS THE PLAYER FROM SPRINTING WHILE THE MEDKIT IS BEING USED
		sprintHeld = input.UserInputState == Enum.UserInputState.Begin
		sprint(sprintHeld)
	end
end

local function updateStaminaUI()
	playergui.staminaGUI.staminaFrame.staminaBar.Size = UDim2.new(math.clamp(stamina / maxStamina, 0, 1), 0, 1, 0)
	playergui.staminaGUI.staminaFrame.staminaText.Text = tostring(math.floor(stamina)) .."/" .. tostring(math.floor(maxStamina))
end

userInputService.InputBegan:Connect(onInput)
userInputService.InputEnded:Connect(onInput)

local function stunDBTimer()
	repeat task.wait() until Stun == 0
	stunDB = true
end -- this function is kinda buggy, i keep getting the sprint fucntion called every heartbeat still

runService.Heartbeat:Connect(function(deltaTime)
	if Stun.Value == 1 and stunDB then
		stunDB = false
		sprint(false)
		stunDBTimer()
	end -- this is something that i tried, walkspeed and FOV dont tween back like they should
	
	if sprinting and humanoid.MoveDirection.Magnitude > 0 then -- drain
		stamina = math.max(0, stamina - drainRate * deltaTime)
		updateStaminaUI()
		--print(math.floor(stamina))
		if stamina == 0 then
			sprint(false)
			exhausted = true
			sprintable = false
			task.wait(5)
			exhausted = false
		end
	else
		if not exhausted then -- regen
			if humanoid.MoveDirection.Magnitude > 0 then
				stamina = math.min(maxStamina, stamina + refreshRate * deltaTime)
			elseif humanoid.MoveDirection.Magnitude <= 0 then
				stamina = math.min(maxStamina, stamina + (refreshRate * 1.5) * deltaTime) -- faster regen while standing still
			end
			if stamina >= 0 then
				updateStaminaUI()
			end
			if stamina >= minStamina then
				exhausted = false
				sprintable = true
				--print(math.floor(stamina))
				if sprintHeld and Stun.Value == 0 then -- another thing that i honestly forgot what this did, it might be what regens stamina while stun is active or what makes the walk anim play instead of sprint anim when stun == 1
					sprint(true)
				end
			end
		end
	end
end)

Any and all help is greatly appreciated, and thank you in advance to anybody who spends their time reading through my ~150 line script

2 Likes

Just add a return

	if Stun.Value == 1 and stunDB then return end
1 Like

this doesnt account for the fov and walkspeed, which are triggered with the sprint() function, it also disables stamina regeneration

1 Like

bump, im fr cooked rn idk what to do

1 Like

Let external scripts control the sprinting mechanic, somehow. Maybe an attribute on the character (or anywhere easily accessible for that matter), or perhaps make the sprinting script a module. Then, via some API you created, you manually interrupt sprinting and also prevent it from being enabled for the duration of the MedKit’s usage (and maybe until it is unequipped?? YMMV).

1 Like

Wow, it took me a long time, but I re-coded the sprinting script using an attribute for stun and was able to get what I wanted, thanks so much I didn’t think about attributes before your message!

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