Script sometimes doesn't run

So this script sometimes works, sometimes doesn’t, it’s all random right now.

No errors on output.

Script:

local t
local b = Instance.new("BindableEvent")
-- Services
local uis = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
-- Variables
local cam = game.Workspace.CurrentCamera
local stamina = game.Players.LocalPlayer:WaitForChild("StatsFolder"):WaitForChild("Stamina")
local char = game.Players.LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local regen = game:GetService("ReplicatedStorage").RemoteEvents.Regen
local stopRunningEvent = game:GetService("ReplicatedStorage").RemoteEvents.StopRunning
-- Events
local staminaRemote = game:GetService("ReplicatedStorage").RemoteFunctions.StaminaEvent
local runStop = game:GetService("ReplicatedStorage").RemoteEvents.StopRunning
-- Functions
local function createtween(obj, newVal)
	local tweenInfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
	local tween = tweenService:Create(obj, tweenInfo, {FieldOfView = newVal})
	tween:Play()
end

local function onRequestReceived()
	b:Fire("received")
end

local function checkRegen()
	while wait(0.35) do
		if stamina.Value ~= 100 then
			if tick() - t >= 3 then
				regen:FireServer()
			end
		end
	end
end
-- Event
uis.InputBegan:Connect(function(input, gpe)
	if gpe == false then
		if uis.KeyboardEnabled == true then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				local canRun = staminaRemote:InvokeServer("run", true)
				if canRun then
					onRequestReceived()
					hum.WalkSpeed = 25
					createtween(cam, 85)
				end
			elseif input.KeyCode == Enum.KeyCode.Space then
				onRequestReceived()
				staminaRemote:InvokeServer("jump", false)
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, gpe)
	if gpe == false then
		if uis.KeyboardEnabled == true then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				staminaRemote:InvokeServer("run", false)
				onRequestReceived()
				hum.WalkSpeed = 16
				createtween(cam, 70)
			end
		end
	end
end)

runStop.OnClientEvent:Connect(function()
	game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed = 16
	createtween(cam, 70)
end)

stamina.Changed:Connect(function(prop)
	if prop == stamina.Value then
		if stamina.Value < 10 then
			hum.JumpPower = 0
		elseif stamina.Value >= 10 then
			hum.JumpPower = 50
		end
	end
end)

b.Event:Connect(function(event)
	if event then
		if event == "received" then
			t = tick()
		end
	else
		error("No event specified.")
	end
end)

stopRunningEvent.OnClientEvent:Connect(function(trig)
	if trig then
		b:Fire()
	else
		staminaRemote:InvokeServer("run", false)
		onRequestReceived()
		hum.WalkSpeed = 16
		createtween(cam, 70)
	end
end)

local f = coroutine.wrap(checkRegen)
f()

Edited the topic. (30 charsss)

If nothing’s showing up in the output, perhaps you should use print, warn and maybe even the debugger to determine what is running and what isn’t.

If you’re not even sure if the script is running from the beginning, print something at the top. If that isn’t happening, then whatever is supposed to allow script to run isn’t doing its job.

1 Like

My bad, script runs. I meant sometimes it doesn’t function properly.

1 Like

Fixed with moving the CharacterAdded event at the beginning of the script.