Script wont enable when player isnt running

local replicatedstorage = game:GetService("ReplicatedStorage")
local StaminaDrainStart = replicatedstorage.RemoteEvents.StaminaDrainStart
local StaminaDrainStop = replicatedstorage.RemoteEvents.StaminaDrainStop
local StaminaRegen = script.Parent.Stats.StaminaRegen

local Draining = false
StaminaDrainStart.OnServerEvent:Connect(function(player)
	Draining = true
	
	local stamina = player:WaitForChild("PvPStats").Stamina
	
	while Draining do 
		StaminaRegen.Disabled = true
		stamina.Value = stamina.Value - 5
		wait(0.75)
	end
end)

StaminaDrainStop.OnServerEvent:Connect(function()
	Draining = false
	StaminaRegen.Disabled = false
end)

Script i want to be enabled

local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
while true do
	wait(1.5)
		local stamina = player.PvPStats.Stamina
		local Character = player.Character
		
		stamina.Value = stamina.Value + 5
		
		local Sprint = Character.Sprint
		
		if stamina.Value < 0 then
			Sprint.Disabled = true
		elseif stamina.Value > 200 then
				stamina.Value = 200
		end
	end
end)

It’s because the script is wrapped in a PlayerAdded function. Therefore when it is enabled, it is waiting for a player to join, but the players have already joined the game, so it will never run.

1 Like