Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.

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
0
its because there is no loop JesseSong 3916 — 3y
0
and where I need to but the loop at SMHerobrine 2 — 3y
0
What do you want the script to do question? JesseSong 3916 — 3y
0
Its a stamina bar scipt. it works in-game but when you die/reset the left shift to sprint dosen't activate SMHerobrine 2 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
--[[
    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.

0
I provided some more info SMHerobrine 2 — 3y
Ad

Answer this question