Local script working in StarterPlayerScripts but not in StarterCharacterScripts

Ok so i have a script that manages stamina. I would like my script to work even after the player died which is not the case curently. The script is in StarterPlayerScripts and it :

-Creates a ContextActionService binding.
-Activates a remote event in ReplicatedStorage for a server script to change a IntValue.
-Tweens a Gui to the size of the IntValue.
-Has variables about the character and humanoid of the player.
-Has no “script.something” variables or references.

The whole script is kinda confidential but i can post little bits if you need to see something.
I tried a lot of things but none worked.
I realy don’t see how to make this work…
Thanks for your help.

1 Like

I think your logic is counter-intuitive. How can we help you if we don’t know what the code is? You’ve given us a brief description, that’s it. It’s like walking into a hospital but refusing to tell them what happened.

We also need to know what errors are occurring, if any.

Sadly, no errors are occuring (it would be easier :smiley:)
please dont use this code anyone

--variables--
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StaminaEvent = ReplicatedStorage.RemoteEvents.Stamina

local StaminaDisplay = Player.PlayerGui.StaminaGui.Background.StaminaDisplay

local stamina = Player.PlayerGui.StaminaGui.Stamina
local running = false
local jumping = false
local regenerating = false


--stamina regen--
local function regen()
	regenerating = true
	wait(1)
	while stamina.Value < 1000 and not running and not jumping do
		StaminaEvent:FireServer(1)
		StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		if stamina.Value > 250 then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
		end
		if Humanoid.MoveDirection.Magnitude == 0 then
			StaminaEvent:FireServer(3)
			StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		end
		wait(0.01)
	end
	StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
	regenerating = false
end

--Jumping--
Humanoid.StateChanged:Connect(function(old,new)
	if new == Enum.HumanoidStateType.Jumping then
		jumping = true
		StaminaEvent:FireServer(-250)
		StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
		wait()
		jumping = false
		if stamina.Value > 250 then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
		end
		if not regenerating then
			regen()
		end
	end
end)



--running--
local function Sprint(actionName,inputState)

	if actionName == "Sprint" and inputState == Enum.UserInputState.Begin then
		running = true
		while stamina.Value > 0 and running do
			if Humanoid.MoveDirection.Magnitude ~= 0 then
				Humanoid.WalkSpeed = 25
				StaminaEvent:FireServer(-4)
				StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
				if stamina.Value < 251 then
					Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
				end
				if stamina.Value < 0 then
					running = false
					Humanoid.WalkSpeed = 16
					if not regenerating then
						regen()
					end
				end
			end
			wait(0.01)
		end
		if stamina.Value < 0 then
			running = false
			Humanoid.WalkSpeed = 16
			if not regenerating then
				regen()
			end
		end


	elseif actionName == "Sprint" and inputState == Enum.UserInputState.End then
		running = false
		Humanoid.WalkSpeed = 16
		if not regenerating then
			regen()
		end
	end
end

--CAS binding--
ContextActionService:BindAction("Sprint",Sprint,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL3)
ContextActionService:SetImage("Sprint","rbxassetid://1488065688")
ContextActionService:SetPosition("Sprint",UDim2.new(1, -90, 0.5, -100))

Scripts in StarterCharacterScripts are destroyed and created again once the character respawns. It would be better to keep your stamina script in StarterPlayerScripts in order to allow the script to work even after a player dies.

You can create a function that updates to the new character once the player dies.

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local Character = player.CharacterAdded:Wait()

local function onCharacterAdded(character)
    Character = character
end

player.CharacterAdded:Connect(onCharacterAdded)
1 Like

Sadly, it still doesn’t work…

it does this error:
21:48:40.673 Can only tween objects in the workspace - Client - StaminaScript:33
21:48:40.673 Stack Begin - Studio
21:48:40.673 Script ‘Players.nico_qwer.PlayerScripts.StaminaScript’, Line 33 - function regen - Studio - StaminaScript:33
21:48:40.674 Script ‘Players.nico_qwer.PlayerScripts.StaminaScript’, Line 93 - function Sprint - Studio - StaminaScript:93
21:48:40.674 Stack End - Studio

It seems like the instance you’re tweening is getting destroyed so its parent is nil and it errors your code. You may have to create a new tween each time it’s destroyed.

local StaminaDisplay = Player.PlayerGui.StaminaGui.Background.StaminaDisplay

The stamina display is being removed when the character respawns. Go to the ScreenGui and disable ResetOnSpawn.

ok i did that and tweaked some stuff.
now it works except for when the player jumps
here is the script now

--variables--
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StaminaEvent = ReplicatedStorage.RemoteEvents.Stamina

local StaminaDisplay = Player.PlayerGui.StaminaGui.Background.StaminaDisplay

local stamina = Player.PlayerGui.StaminaGui.Stamina
local running = false
local jumping = false
local regenerating = false


--stamina regen--
local function regen()
	regenerating = true
	wait(1)
	while stamina.Value < 1000 and not running and not jumping do
		StaminaEvent:FireServer(1)
		StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		if stamina.Value > 250 then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
		end
		if Humanoid.MoveDirection.Magnitude == 0 then
			StaminaEvent:FireServer(3)
			StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		end
		wait(0.01)
	end
	StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
	regenerating = false
end

--Jumping--
Humanoid.StateChanged:Connect(function(old,new)
	if new == Enum.HumanoidStateType.Jumping then
		jumping = true
		StaminaEvent:FireServer(-250)
		StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
		wait()
		jumping = false
		if stamina.Value > 250 then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
		end
		if not regenerating then
			regen()
		end
	end
end)



--running--
local function Sprint(actionName,inputState)

	if actionName == "Sprint" and inputState == Enum.UserInputState.Begin then
		running = true
		while stamina.Value > 0 and running do
			if Humanoid.MoveDirection.Magnitude ~= 0 then
				Humanoid.WalkSpeed = 25
				StaminaEvent:FireServer(-4)
				StaminaDisplay:TweenSize(UDim2.new (stamina.Value / 1000,0,1,0), "Out", "Linear", 0.1)
				if stamina.Value < 251 then
					Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
				end
				if stamina.Value < 0 then
					running = false
					Humanoid.WalkSpeed = 16
					if not regenerating then
						regen()
					end
				end
			end
			wait(0.01)
		end
		if stamina.Value < 0 then
			running = false
			Humanoid.WalkSpeed = 16
			if not regenerating then
				regen()
			end
		end


	elseif actionName == "Sprint" and inputState == Enum.UserInputState.End then
		running = false
		Humanoid.WalkSpeed = 16
		if not regenerating then
			regen()
		end
	end
end

--CAS binding--
ContextActionService:BindAction("Sprint",Sprint,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL3)
ContextActionService:SetImage("Sprint","rbxassetid://1488065688")
ContextActionService:SetPosition("Sprint",UDim2.new(1, -90, 0.5, -100))

local function onCharacterAdded(character)
	Character = character
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
	Humanoid = Character:WaitForChild("Humanoid")
	running = false
	jumping = false
end

Player.CharacterAdded:Connect(onCharacterAdded)

You only checked the jumping stamina once. I’m not sure how the regen() function works so you’ll have to edit that.

		wait()
		jumping = false
		while stamina.Value < 250 then
			regen()
		end
		
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)

i tried writing what you wrote but it didn’t work…
also, i don’t understand what you mean by:

plus, my regen() is simple:
it is a while loop that adds stamina if stamina < 1000 and if the player isn’t running or jumping

You set Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) so your if new == Enum.HumanoidStateType.Jumping then would only fire once and never again because of your

if stamina.Value > 250 then
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
end

not resetting the humanoid state since stamina will be less than 250.

yeah, but regen() tests for

if stamina.Value > 250 then
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true)
end

every loop so it should work
it works on first spawn, but if you die, the function that calculates stamina for jumping doesn’t work…

I found what i did wrong!
(I still dont have a way to fix it tho)
when I say:

Humanoid.StateChanged:Connect(function(old,new)

the code checks if the state of the humanoid changes right?
but if i change the humanoid variable, it will still look for the old humanoid

I need a way to reset this line or the whole function