#need help with this bug.

1 messages · Page 1 of 1 (latest)

static birch
#
        -- play random horror sounds (coded by mich)
        local horrorsoundsFolder = game.Workspace.HORRORSOUNDS
        local horrorsoundsCount = 1 -- the max amount of horror sounds that can be played at once
        -- note: changing the count more than horror sounds in HORRORSOUNDS folder can cause "not a valid member" error
        local Random = math.random(1, horrorsoundsCount) -- this variable will be used for choosing random number
        
        while true do
            wait(20) -- just wait for a while before playing one
            horrorsoundsFolder.HORROR[Random].Playing = true
            print("Random horror sound is played")
            -- you can change the horrorsoundsCount if you added new horror sound -mich
        end
#

Output:

  11:45:16.392  Stack Begin  -  Studio
  11:45:16.393  Script 'Workspace.Cupcake.Script', Line 119  -  Studio - Script:119
  11:45:16.393  Stack End  -  Studio
upper mountain
static birch
#

for some reason it skips "Random" variable

#

look, its supposed to choose random number after "HORROR"

lucid vigil
#

horrorsoundsFolder[Random].Playing = true

#

HORROR dosn't existe, you only have "HORROR1" and if you want to select a child with the ID you put it in de folder name

#
horrorsoundsFolder[Random].Playing = true
static birch
#

it was suppposed to choose random number after the name "HORROR", if you look at horrorsoundsCount variable and Random variable, you'll see the problem

lucid vigil
#

If you have more than one sound in the floder its going to chose one between 1 and 1 so the Random variable its Useless

hoary ironBOT
#

studio** You are now Level 2! **studio

static birch
#

okay well im gonna add another horror sound and see if this problem is fixed

lucid vigil
static birch
#

hang on let me test first then try your suggestion

floral gale
#

Bruh another AI generated script

lucid vigil
#

Or create local variable before like this

        local horrorsoundsFolder = game.Workspace.HORRORSOUNDS
        local horrorsoundsCount = 2 -- the max amount of horror sounds that can be played at once
        -- note: changing the count more than horror sounds in HORRORSOUNDS folder can cause "not a valid member" error
        local Random = math.random(1, horrorsoundsCount) -- this variable will be used for choosing random number
        
        while true do
            wait(20) -- just wait for a while before playing one
            local newSoundPath = "HORROR".. Random
            horrorsoundsFolder[newSoundPath].Playing = true
            print("Random horror sound is played")
            -- you can change the horrorsoundsCount if you added new horror sound -mich
        end
floral gale
#

Do you even know what the script does ?

static birch
#

ok it didnt work

static birch
floral gale
#

Tutorial ?

static birch
#

i added some messages to the script

static birch
floral gale
static birch
#

btw i was helping my friend on his own game

static birch
lucid vigil
#

It's should work

static birch
floral gale
static birch
lucid vigil
#

It's work

#

You have to reset RANDOM variable every time you want to change the sound

static birch
#

wait it worked

hoary ironBOT
#

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

lucid vigil
#
local horrorsoundsFolder = game.Workspace.HORRORSOUNDS
local horrorsoundsCount = 2 

while true do
    wait(2) -- just wait for a while before playing one
    local Random = math.random(1, horrorsoundsCount)
    local newSoundPath = "HORROR".. Random
    horrorsoundsFolder[newSoundPath].Playing = true
end
lucid vigil
static birch
#

btw that game is made by my friend but for some reason he used ai to code horror mode function so yeah idk why

lucid vigil
upper mountain
#

Another day another one of these

static birch
static birch
# floral gale Do you even know what the script does ?

let me explain this, so firstly i added variables that gets horror sounds folder then another variable which keeps track of how many horror sounds are there in the folder. theres also random variable that chooses from 1 to horrorsoundsCount (which is 2). Then it enters the loop where it waits for while then plays random horror sound, btw i also added print because i want to know if my code is working correctly (or just for debugging). So do you believe me now? (sorry for ping 😦 )

lucid vigil
#

It's work ?

static birch
#

yes

static birch
#

it worked with ["HORROR".. Random]

lucid vigil
static birch
#

ok

#

there changed it, now for last test then ill remove that print statement

#

btw do u want to see the horror game that my friend made (thats with my code that plays random horror sounds)?

static birch
#

okay it worked

static birch
#

@lucid vigil for some reason, the horror sounds keeps playing without waiting for 50 seconds

lucid vigil
#

You can still use wait(50) or task.wait(50) but I recommande this :

static birch
#

task.wait(50)? alr

static birch
#

@lucid vigil it still plays sounds without waiting

lucid vigil
#
local horrorsoundsFolder = game.Workspace:WaitForChild("HORRORSOUNDS")
local horrorsoundsCount = 2
local currentSound = nil

task.wait(1)

while true do
    if currentSound ~= nil  then
        wait(50)
        currentSound = nil
    else
        
        local Random = math.random(1, horrorsoundsCount)
        local newSoundPath = "HORROR".. Random
        currentSound = horrorsoundsFolder[newSoundPath]
        horrorsoundsFolder[newSoundPath].Playing = true
        warn("Playing sound : " ..currentSound.Name)
        
    end
end
#

@static birch use that to see

#

modifie task.wait if you want to put 1 seconde

hoary ironBOT
#

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

static birch
#

@lucid vigil heres output:

  13:17:48.075  Stack Begin  -  Studio
  13:17:48.075  Script 'Workspace.Cupcake.Script', Line 113  -  Studio - Script:113
  13:17:48.075  Stack End  -  Studio
lucid vigil
#

new update

static birch
#

k

hoary ironBOT
#

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

static birch
#

it still wont wait for 20 secs @lucid vigil

  13:24:40.492  Playing sound : HORROR2  -  Server - Script:128
  13:24:43.858  Playing sound : HORROR1  -  Server - Script:128
  13:24:49.359  Playing sound : HORROR2  -  Server - Script:128
lucid vigil
#

Its depend of the lenght of the sound @static birch

#

If the sound don't finish its won't start another

static birch
#

but its supposed to wait for 50 seconds after the horror sound is over then plays another horror sound

static birch
#

okay

#

well if it didnt work then ill try to fix it ig

lucid vigil
#

So ?

static birch
#

okay it still didnt wait

#

it waited until the horror sound is over then plays another

#

i dont know why roblox studio is doing this to us

#

ok lemme try to fix it

lucid vigil
#

ok! say me if you are make it done

static birch
#

lemme recode it

static birch
crisp cosmos
#

why dont u just use the # of sounds in the folder to determine what can be played randomly? in your original code your max sounds played at once doesnt determine how many sounds can be played at once, but how many sounds you have in the folder (which could be inaccurate) so u sometimes get the not a member error

static birch
#

plus, I'm gonna recode it right now i guess

crisp cosmos
#

local horrorsoundsFolder = game.Workspace.HORRORSOUNDS

local Random = math.random(1, #horrorsoundsFolder:GetChildren()) --
while true do
wait(2) -- just wait for a while before playing one
horrorsoundsFolder:GetChildren()[Random].Playing = true
print("Random horror sound is played")
end
crisp cosmos
#

lowkey could be improved but i feel the original is more demonstrative of what is going on, heres how i would leave it:

local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()


while true do
local Random = math.random(1, #tableofsounds ) --
wait(2) -- just wait for a while before playing one
tableofsounds[Random].Playing = true
print("Random horror sound is played")
end
#

oh also i forgot to make the random call in the while loop, make sure thats in there or ur gonna get the same sound every time

crisp cosmos
#

also depending on if u want the sound to restart or not u could do :Play() instead of .Playing = true

static birch
#

lemme try with wait(50)

#

@crisp cosmos for some reason, after 50 seconds it plays horror sounds too quickly without waiting for 50 seconds

#
     13:59:52.944     Random horror sound is played
     13:59:56.892     Random horror sound is played```
#

what if i put this piece of script in module script then let the main script trigger it

#

lemme try my idea, be right back

crisp cosmos
#

are u sure ur not calling this piece of code several times

#

is this like an independent script or is something calling a function that does this

static birch
#

btw heres module script, does this look good?

local PlayRandomHorrorSounds = {}

-- play random horror sound remake (by mich)
local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()

function PlayRandomHorrorSounds.Trigger()
    -- play random horror sound remake (by mich)
    local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()

    while true do
        local Random = math.random(1, #tableofsounds ) --
        wait(50) -- just wait for a while before playing one
        tableofsounds[Random].Playing = true
        print("Random horror sound is played")
    end
end

return PlayRandomHorrorSounds
crisp cosmos
#

makes sense, it shows up pretty regularly every 4 seconds

#

if i was approaching making a module to play horror sounds i would personally remove the waits and the while loop from the module

#

since the idea is flexibility, im going to remove as many things that restrict me as possible

#

that would leave me with a module that, for example, could produce a horror sound from a preset list anywhere (without locking me in to doing it forever every 50 seconds)

#

also when making a module script and making a function that can be used by other code you need to declare it differently. we need to add it to the table that we return when the module is required, so like this:

static birch
#

in main script:

local PlayRandomHorrorSoundsModule = game.Workspace.PlayRandomHorrorSounds

-- rest of code

        -- play random scary ahh sounds module
        PlayRandomHorrorSoundsModule.Trigger()

-- rest of code
crisp cosmos
#
local PlayRandomHorrorSounds = {}

PlayRandomHorrorSounds.Trigger = function(partToPlayIn)
--update potential sounds every time we do this
local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()
local Random = math.random(1, #tableofsounds)
if partToPlayIn then
local specificplace = tableofsounds[Random]:Clone()
specificplace.PlayOnRemove = true
specificplace.Parent = partToPlayIn
specificplace:Destroy()
else
  tableofsounds[Random]:Play()
end
end

return PlayRandomHorrorSounds
#

so is the sound supposed to be like

#

an abstract sound or come from a specific part?

static birch
#

yeah uh after playing the sound, it waits for 50 seconds before playing another one

crisp cosmos
#

i mean like do they hear it come from somewhere specific is the idea

#

or just all around them

static birch
#

the sounds play in workspace itself in folder HORRORSOUNDS

#

idk maybe join vc and ill show you?

crisp cosmos
#

so the module i just made has the option of doing either

#

if you call Trigger(part) it will place it in that specific part

#

in the regular script you would do this:

local soundmodule = require(blah.PlayRandomHorrorSounds)
while true do
task.wait(50)
  soundmodule.Trigger()
end
static birch
#

what?? but i did same thing as in my other horror game and it worked, but it doesnt work in this version

  14:18:47.814  Trigger is not a valid member of ModuleScript "Workspace.PlayRandomHorrorSounds"  -  Server - Script:115
  14:18:47.814  Stack Begin  -  Studio
  14:18:47.814  Script 'Workspace.Cupcake.Script', Line 115  -  Studio - Script:115
  14:18:47.814  Stack End  -  Studio
#

how is it not valid member?

#

i just did Trigger()

#

bru I made one of the most common beginner mistakes, wrong symbol..

hoary ironBOT
#

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

static birch
#

instead of . i changed it to :

crisp cosmos
#

rip

static birch
#

and same error

crisp cosmos
#

post code

static birch
#

"Trigger is not a valid member of ModuleScript"

#

it points to this line

PlayRandomHorrorSoundsModule:Trigger()
crisp cosmos
#

still says :

#

post all the code tho

static birch
#

i literally cant send because its too long

#

why discord

#

wait

crisp cosmos
#

you didnt require the module

static birch
#

forgot to put require(

#

i looked at script in my other horror game

#

yeah i realized that

#

test #unknown (i lost count)

#

bro i swear to god i did not type that

#

@crisp cosmos the bug didnt get fixed, it still plays sounds too quickly

static birch
#

oh wait theres a mistake, ill fix it

#

@crisp cosmos another error, i have no idea why roblos studio is even doing this to us

  14:50:03.540  Stack Begin  -  Studio
  14:50:03.540  Script 'Workspace.PlayRandomHorrorSounds', Line 10 - function Trigger  -  Studio - PlayRandomHorrorSounds:10
  14:50:03.540  Script 'Workspace.Cupcake.Script', Line 115  -  Studio - Script:115
  14:50:03.540  Stack End  -  Studio```
crisp cosmos
#

show the whole code

static birch
#
local PlayRandomHorrorSounds = {}

function PlayRandomHorrorSounds.Trigger(partToPlayIn)
    --update potential sounds every time we do this
    local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()
    local Random = math.random(1, #tableofsounds)
    if partToPlayIn then
        local specificplace = tableofsounds[Random]:Clone()
        specificplace.PlayOnRemove = true
        specificplace.Parent = partToPlayIn
        specificplace:Destroy()
    else
        tableofsounds[Random]:Play()
    end
end

return PlayRandomHorrorSounds
crisp cosmos
#

what are you passing as partToPlayIn

static birch
crisp cosmos
#

no like when u call it

#

the server scipt

static birch
#

do i place "partToPlayIn" inside that

crisp cosmos
#

not necessarily

#

its optional and lets you specify a part in the workspace for the sound to come out of

static birch
#

i wanted the horror sounds to actually play in the background

crisp cosmos
#

yeah so dont include it

#

itll just play as normal

static birch
#

okay lemme use this once again

local PlayRandomHorrorSounds = {}

-- play random horror sound remake (by mich)
local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()

function PlayRandomHorrorSounds.Trigger()
    -- play random horror sound remake (by mich)
    local tableofsounds = game.Workspace.HORRORSOUNDS:GetChildren()

    while true do
        local Random = math.random(1, #tableofsounds ) --
        wait(50) -- just wait for a while before playing one
        tableofsounds[Random].Playing = true
        print("Random horror sound is played")
    end
end

return PlayRandomHorrorSounds
#

I STILL DONT UNDERSTAND

     15:48:55.716     Random horror sound is played
     15:49:02.414     Random horror sound is played
     15:49:06.931     Random horror sound is played```
#

EVEN WITH wait(50) IT JUST DOESNT WAIT

#

okay i think roblox studio hates me

#

also even the whole main script that my friend put is made by AI, so i dont even understand what the code literally does unless I ask him. But hes not online

static birch
#

@crisp cosmos do you understand what the main script (not module script) does? because i dont

static birch
#

oh i finally figured it out, it turns out that it triggered the module script everytime i press on cupcake

floral gale
#

Is he trying to make a game or to learn scripting ?

#

Either way he’s wasting his time if he attempts to succeed in any of the options above.

static birch
#

It literally was only playing a horror sound

static birch
#

Bruh he said that ai is making tween function

hoary ironBOT
#

studio** You are now Level 6! **studio

static birch
#

It's possible that the ai is messing up the code or smth

floral gale
#

Send script

static birch
#

But he literally removed that function completely

floral gale
#

What were y’all expecting besides destroying your own project with AI ?

static birch
#

Well I have to recode it all over again and I still remember how to code that

#

Well his horror game worked so perfectly

#

Like uh there was no error, but if someone used AI then it means they are lazy

#

Brb I'ma code my function

static birch
#

Well I did use ai before but I have learned some lua scripts from the ai

#

I think the ai is used for learning lua

#

Not coding the lua

#

I'ma code it then send the script

#

@floral gale heres the code that i wrote by myself:

        -- play some random ahh spooky sounds remake edition (by mich)
        local HORRORSOUNDSFolder = game.Workspace.HORRORSOUNDS
        local HORRORSOUNDSCount = #HORRORSOUNDSFolder:GetChildren()
        local randomHorrorSoundsSELECT = math.random(1, HORRORSOUNDSCount)
        local newHorrorSoundPATH = "HORROR".. HORRORSOUNDSCount
        HORRORSOUNDSFolder[newHorrorSoundPATH].Playing = true
#

oh wait i messed up something!

#

heres fixed code:

        -- play some random ahh spooky sounds remake edition (by mich)
        local HORRORSOUNDSFolder = game.Workspace.HORRORSOUNDS
        local HORRORSOUNDSCount = #HORRORSOUNDSFolder:GetChildren()
        local randomHorrorSoundsSELECT = math.random(1, HORRORSOUNDSCount)
        local newHorrorSoundPATH = "HORROR".. randomHorrorSoundsSELECT
        HORRORSOUNDSFolder[newHorrorSoundPATH].Playing = true
#

lemme test