Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make this Local Script work after you die/reset?

This is a stamina bar (local) script for my game. But if you reset/die the left shift to sprint doesn't work. If you want to see how it works please play my game (https://www.roblox.com/games/6750846558/SCP-Foundation-ALPHA?) to see how it works. The Script is down below. images here (https://ibb.co/RhtvgwP) of the file system. NOTE that the GUI is cloned at the start of the game into game.players.(player_name).PlayerGui

local bar = script.Parent

local stamina = 100
local staminaRate = 1


local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local humanoid = char:WaitForChild("Humanoid")


local isSprinting = false


local uis = game:GetService("UserInputService")


uis.InputBegan:Connect(function(key, gameProcessed)
	
	if gameProcessed or stamina == 0 then return end
	
	
	if key.KeyCode == Enum.KeyCode.LeftShift then
		
		isSprinting = not isSprinting
		
		if isSprinting then
			humanoid.WalkSpeed = 25
		else
			humanoid.WalkSpeed = 16
		end
	end
end)


while wait() do
	
		
	if stamina == 0 and isSprinting then
		
		isSprinting = false
		humanoid.WalkSpeed = 16
	end
	
	
	if isSprinting and humanoid.MoveDirection.Magnitude > 0 then
		
		stamina = stamina - 1
		wait(staminaRate)
		
	else
		stamina = stamina + 1
		wait(staminaRate)
	end
	
	
	stamina = math.clamp(stamina, 0, 100)
	
	bar:TweenSize(UDim2.new((1/100) * stamina, 0, 1 ,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.5)
end

4 comments

0

its because there is no loop

0

and where I need to but the loop at

0

What do you want the script to do question?

0

Its a stamina bar scipt. it works in-game but when you die/reset the left shift to sprint dosen't activate

1 answer

--[[
	If you want to make a peice of code run on the player's death,
	check their humanoid state.
]]

-- I would recommend placing this in a LocalScript @ StarterPlayer.StarterPlayerScripts

repeat
	wait()
until game:IsLoaded() -- Wait until the game loads, then we can attach it.

local OnCharacterAdded; OnCharacterAdded = function(Character)
	repeat
		wait()
	until Character:FindFirstChildWhichIsA("Humanoid") -- Wait for a Humanoid.
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	local OnDeath; OnDeath = function()

	end
	Humanoid.StateChanged:Connect(function(LastHumanoidStateType, CurrentHumanoidStateType)
		if CurrentHumanoidStateType == Enum.HumanoidStateType.Dead then
			coroutine.wrap(OnDeath)() -- Run this as a separate thread, because if you're interested in running yielding functions in OnDeath; anything after this point won't be delayed.
		end
	end)
end

local Player: Player = script:FindFirstAncestorWhichIsA("Player")

if Player.Character then
	pcall(OnCharacterAdded, Player.Character)
end

Player.CharacterAdded:Connect(OnCharacterAdded)

This may not meet your question's standards, I would suggest you provide more information.

1 comment

0

I provided some more info

This discussion is preserved as it was. New answers, comments, and votes are closed.