#How to make multiple functions run multiple times at once

1 messages · Page 1 of 1 (latest)

buoyant pumice
#

I have a tower defence game in the making, and my issue is that when i place multiple towers they interfere with each other in the tower.CheckAttack and tower.Attack loop and in the functions received by remote events.

#

Above is all server side
Below is all client side

dawn halo
#

define "they interfere with each other"

buoyant pumice
# dawn halo define "they interfere with each other"

When i place one tower and let an enemy go into range, the entire script works perfectly find. If i add a second tower somewhere else, the shooting sfx will play for other towers, the attack beams will be all over the place and be activated when they shouldnt. etc

#

this is as intended

dawn halo
#

how do u create new tower objects?

#

make sure you are creating new tower objects and not referencing the same one

buoyant pumice
#

this one is infinitely playing an sfx sound when it shouldnt

buoyant pumice
dawn halo
buoyant pumice
#

i dont have that key on my kb can you send it me for copypaste

dawn halo
#

if you dont have ` in your keyboard hold alt+9+6 in that order

buoyant pumice
#

uhhhh

#

i got an error for trying to send it as text 😭

#

ill seperate it into 2 parts

dawn halo
buoyant pumice
#
-- CREATES TABLE
local tower = {}

local isAllowedSpawn = false

-- CREATES NEW TOWER FOR PLACEMENT AND UPGRADES
function tower.Spawn(player, name, cframe, previous, isUpgrade)
    local map = workspace.Map1
    
    local allowedSpawn = isUpgrade
    if isUpgrade then
        isAllowedSpawn = true
    elseif not isUpgrade then
        local allowedSpawn = tower.CheckSpawn(player, name)
        local map = workspace.Map1
        if allowedSpawn    then
            isAllowedSpawn = true
        end
    end
    
    if isAllowedSpawn then
        isAllowedSpawn = false
        
        local newTower
        local oldMode = nil
        if previous and previous.Config.Upgrade.Value ~= nil then
            newTower = previous.Config.Upgrade.Value:Clone()
            if previous.Config.TotalCost then
                newTower.Config.TotalCost.Value = previous.Config.TotalCost.Value + newTower.Config.Price.Value
            end
            oldMode = previous.Config.TargetMode.Value
            stopAttack = true
            stopAttackEvent:Fire(newTower)
            task.wait(0.1)
            previous:Destroy()
            
        else
            newTower = ReplicatedStorage.Towers[name]:Clone()
            local totalCost = newTower.Config.TotalCost
            totalCost.Value = newTower.Config.Price.Value
            player.Towers.PlacedTowers.Value += 1
        end
#

why is it so messed up

#

its fine im sure its good

dawn halo
#

at this point just send a .txt file with the code XD

buoyant pumice
#

can i do that?

#

oh damn

dawn halo
buoyant pumice
#

thats how thats done

#

it only did the bottom half

#

BRUHH

#

1 sec

dawn halo
#

pro tip, press ctrl+a to select the entire text

#

and ctrl+c to copy it

buoyant pumice
dawn halo
#

and ctrl+v to paste it

buoyant pumice
#

but ctrl a will give you 1000 lines of code

#

even after looking myself i cant see the problem in the code at all

finite dust
#

task.spawn / coroutine.wrap

#

i would use task.spawn

#

it’s easy

#

also if you’re going to add task.wait() inside that task.spawn

#

just use task.delay

buoyant pumice
#

iv heard of task.delay what does it do

finite dust
#

it’s like

#

task.delay(3, function()
print(“hey”)
end)

#

ts plays after 3 second

#

and it wont stop your function

#

it will go ahead

buoyant pumice
#

and for task.spawn, would i do it like "task.spawn(tower.CheckAttack)(newTower, player)

finite dust
#

nah

#

task.spawn(function()
— your script here
end)

#

this

#

do it

#

bye

#

ima go sleep

buoyant pumice
#

ill try it, cheers

#

@dawn halo you think this wil work?

dawn halo
buoyant pumice
dawn halo
#

but with this little code i cant really proove or deny it xd

buoyant pumice
#

how would i go around making them fully unique to each other

dawn halo
gloomy boneBOT
#

studio** You are now Level 12! **studio

dawn halo
#

the biggest suspect is that entire module

verbal steeple
buoyant pumice
#

wait

#

this might help

#

all the towers attacked individually and worked until i changed something in these 2 blocks of code:

verbal steeple
#

yall going in loops here

buoyant pumice
#

wdym?

verbal steeple
buoyant pumice
#

theres 2 in there

verbal steeple
buoyant pumice
#

animateTowerEvent:FireAllClients(newTower, "Attack", target)

verbal steeple
#

i assume CheckAttack is called for every tower

buoyant pumice
#

once a tower is added to the game, it calls it

verbal steeple
buoyant pumice
#

let me check

#

no

verbal steeple
#

if it does then it is in fact attacking and your range check is broken

buoyant pumice
#

it doesnt take bonus damage, but it does play the attack sfx too many times and doesnt remove them when theres more than 1 tower

verbal steeple
#

first off, we know the remote is firing multiple times. I assume this is the only way for the effects to fire like that. you can add a print in there to confirm

verbal steeple
buoyant pumice
#

when there is 1 tower, all works perfect, when there are 2 towers, no matter how far apart or where they are or what they see, the tower that is attacking and the tower that isnt will both play attacking sfx and not remove them after. Then sometimes the beams also bug out

buoyant pumice
verbal steeple
verbal steeple
buoyant pumice
#

So the script with the tower.CheckAttack and tower.Attack are both in my Tower Module script located in serverscriptservice under the Main script

#

the script that is recieving the event is my animations local script, located in starterPlayer under starterplayerscripts

verbal steeple
#

doesn't seem like any duplicates there so far

buoyant pumice
#

the issue has to be on the animation side, because the only problems im having are with the animations

verbal steeple
# buoyant pumice

at this point it'd be faster to just test and debug. add some prints to the receiver here and see how many times it actually fires. that should tell a lot.

buoyant pumice
verbal steeple
#

and how many times does it fire

buoyant pumice
#

well

#

when i place a tower it does this

#

it will check for a target every 0.1s in the while loop in checkattack

#

then when an enemy comes into contact

#

it will spam this

#

every hit

verbal steeple
#

slow down

buoyant pumice
#

this is for 1 tower

#

and 2 towers:

verbal steeple
#

you have too many prints here i think, i dont know what the others are, only 'Found Target in Animation'

verbal steeple
#

the only prints you need are where the sound:play is, at the top, before any conditions

#

like if target then print() is not helpful

buoyant pumice
#

okay 1 sec

gloomy boneBOT
#

studio** You are now Level 7! **studio

verbal steeple
# buoyant pumice

although i must say, if this is only a single connection, pawnSoundIsPlaying is a debounce that is shared between all towers, but it shouldn't play the sound twice, it should just destroy them

buoyant pumice
verbal steeple
#

maybe add teh tower to it

#

print("Sound is playing", tower)

buoyant pumice
#

ohhh

#

wait

#

im stupid

#

wrong function

#

1 sec

#

taht was checking for queen attack not pawn

#

okay

#

with 1 tower:

dawn halo
buoyant pumice
#

with 2 tower:

dawn halo
#

to see if it is one or both

buoyant pumice
#

seems like with 1 tower it is recognising that the sound is playing

#

but with 2 its not

verbal steeple
verbal steeple
# buoyant pumice

add the identifier to this so we can see which tower it is playing the sound for

buoyant pumice
#

okay 1 sec

#

do i do them seperate?

#

uhhhh

#

i need a plugin for this?

verbal steeple
#

lol

#

i didnt think that was accessible

dawn halo
#

right i forgor, that is only for plugins T_T

verbal steeple
#

just use tower:GetFullName()

#

and hopefully the 2 towers have different names

#

lol

buoyant pumice
#

with 2 towers:

verbal steeple
#

they probably have the same name lol

#

when you create the tower just give it a number on the end

#

like math.random(1,65535)

buoyant pumice
#

but that has a chance to give them the same identifier

verbal steeple
#

it's unlikely enough to not matter

#

this is debug only

buoyant pumice
#

ill do math.rand for now and then sort it later

#

so just before i clone it?

#

no after.

#

😭

verbal steeple
buoyant pumice
#

bro i had a blonde moment

#

chill

verbal steeple
#

if you do, then give it an attribute with the id kek

buoyant pumice
#

like this

verbal steeple
#

hopefully only one tower shows up

#

if both show up then we have identified the problem

buoyant pumice
#

well we were wrong

#

let me print the newtowers name right after i give it the name

#

WAIITTTT

verbal steeple
buoyant pumice
#

i may have found the problem with the creation

verbal steeple
#

why is it the same tower that's strange

verbal steeple
#

you're welcome

buoyant pumice
#

it wasnt giving it a new id because it was creating the tower with the new id if its an upgrade

#

the whole issue isnt solved

#

😭

verbal steeple
#

aw

buoyant pumice
#

okay so lets test

#

yes to unique identifies

#

no to fixingthe issue

verbal steeple
#

no dude that wasn't to fix it, that was just to help identify the problem

buoyant pumice
#

i know just making sure i relay all information

verbal steeple
# buoyant pumice

if the towers have the same name in the same folder we can't tell if it's both towers playing the sound here or just one

buoyant pumice
#

but i want both to play the sound, but only when that specific tower attacks

#

instead of only when 1 attacks

verbal steeple
#

y'know, your fail condition at the moment

buoyant pumice
#

1 tower, works as expected

#

2 tower, my ears are hurting

verbal steeple
#

show the output..

buoyant pumice
#

oh shit my bad

verbal steeple
#

don't need a blow-by-blow

buoyant pumice
#

1 tower

#

2 tower

verbal steeple
#

seems like it's not coming from the attack then

buoyant pumice
#

should i print tower name before every print?

#

maybe clear the air a bit

verbal steeple
#

you can include it in the print

#

like print(a,b) use a comma, not ..

#

print(tower.Name,"did a thing")

buoyant pumice
#

ah okay

#

my bad im used to html 😭

verbal steeple
buoyant pumice
#

my ui is sexy tho

#

okay let me run test

#

they are both checking for target which is good

#

and the right one found the target, but the other pawn is finding the target in animation

#

when its not attacking

#

so its happening after the event?

verbal steeple
#

no its happening somewhere else

buoyant pumice
#

huh?

#

explain

verbal steeple
# buoyant pumice

well there's no sound coming from that specific sound:Play() because you have a print right next to it

#

so maybe it's coming from a different one

#

ctrl+shift+f for :Play() and check all the sound ones

#

imo

#

like tbh at this point i would have a look at the project but you aren't paying me so

buoyant pumice
verbal steeple
#

and i doubt you can afford to hire anyone

buoyant pumice
#

this one but this is on the queen

verbal steeple
buoyant pumice
#

which only fires if tower is a queen

#

okay

#

theres 2

verbal steeple
#

that's the only 2 :Play in the whole codebase?

buoyant pumice
#

oh no, just in the animations

#

let me keep looking

verbal steeple
#

find all

buoyant pumice
#

should i do it for sounds that arnt even related to that one?

#

like the placing sound

#

in an entirely untouched piece of code for this

verbal steeple
buoyant pumice
#

😭

#

wanna jump in?

verbal steeple
#

not really

buoyant pumice
#

fair play

verbal steeple
#

besides i'd probably lock you out of your own project because age checks

buoyant pumice
#

why, your acc underage or something?

verbal steeple
#

no, yours is

buoyant pumice
#

???

verbal steeple
#

anyway best advice i can give you is just keep debugging

#

keep tracing code

buoyant pumice
#

bro im in my 30s 😭

verbal steeple
#

add prints next to sound:play()

buoyant pumice
verbal steeple
#

being roblox and all

buoyant pumice
#

fair play

#

yes so

#

i did it

verbal steeple
#

but ya debugging is a whole entire skill set

buoyant pumice
#

and the only sound plays related to it are in animations

verbal steeple
#

writing code is more reading code than writing it.

buoyant pumice
#

other than that there are 3 more that look for audio.ErrorSound

#

completely unrelated

#

so its just the 2 :Play's

verbal steeple
#

another thing you can do as part of debugging is have a look through the explorer while the game is running to make sure everything checks out

buoyant pumice
#

true

verbal steeple
#

like honestly if the :play's are only running once as they should then i'm not sure where else it is coming from, short of you missed one, or you're cloning the sound while it's playing

buoyant pumice
#

anyways with the new prints its still only playing the pawnsound

verbal steeple
#

coz sounds don't magically autostart

#

and don't be afraid to write code just to test your other code

#

like for example generating a random number onto the tower's name just to distinguish which tower it was that was playing the sound

#

keep going with things like that

buoyant pumice
#

the pawn thats attacking:

verbal steeple
#

don't just guess or assume

buoyant pumice
#

the pawn tahts not attacking:

verbal steeple
buoyant pumice
#

but it only clones too many when 2 towers

verbal steeple
#

your assumption of the problem was wrong

#

your code isn't running on both towers, it's just one

#

and that was shown earlier with the prints

buoyant pumice
#

ahh okay

#

so how do i go around making it run on both

#

im so shit with client stuff

verbal steeple
buoyant pumice
#

i see

#

so why is it running on both?

#

isnt*

verbal steeple
buoyant pumice
#

im confused

verbal steeple
#

the second tower is probably cancelling out the debounce only for the first tower to immediately start it again

#

like i said you can't just guess dude

buoyant pumice
#

ahhh

verbal steeple
#

you have to prove literally everything

buoyant pumice
#

im aware

verbal steeple
buoyant pumice
#

so how do we check if thats what is actually happening

verbal steeple
#

add debug code to your code to prove things

#

prints go a long way

verbal steeple
#

mind you, i have no idea what this sound is, how long it is, or anything else like that

buoyant pumice
#

its like 4 seconds long, and its looped

#

its an electric sound

verbal steeple
#

well there you go

#

had i know that, this would've been easier

#

so you've got the tower repeatedly playing the sound while it's already playing

buoyant pumice
#

but the sound needs to be looped, thats how the effect works

verbal steeple
#

every 0.5 seconds by the look of it

#

now you know what's happening, you need to figure out why

#

my guess is bad debounce, you don't track the state of the sound properly (or at all)

#

but again thats just a guess you need to actually go in and prove it

#

ya believe it or not most problems i can't just glance at 1000 lines and go "yep there's the problem"

#

it takes a lot of time and effort to trace these issues, add prints to check everything, prove everything is or isn't working as expected.

#

sometimes this can take days, weeks even

buoyant pumice
#

but its good to have a second opinion from someone who is more experienced when you are stuck

#

now im hella confused, how do you get multiple towers to know if a sound is playing for itself and not others

dawn halo
#

Pyro-Fire == best scripter ever

verbal steeple
#

i did spot that earlier but i couldn't confirm since the script might have been cloned per tower which would've made it not an issue

buoyant pumice
#

bro im so lost

stuck dock
buoyant pumice
#

im using a bindable function to try and find out if the sound is playing, and i dont know how to get it to work

verbal steeple
#

aka learn more OOP

buoyant pumice
#

anyways, ignoring OOP, how do you clone a script per tower???

verbal steeple
#

that's a hint on how to do this the beginner naive way

#

the big brain method is to use proper object patterns

#

like if not towerObject:IsSoundPlaying() then towerObject:PlaySound() end etc you can do all sorts

#

but you don't have that, you only have the instance

verbal steeple
#

since the instance tree is a viable substitute for proper code objects

buoyant pumice
verbal steeple
buoyant pumice
#

whats an instance tree?

verbal steeple
#

and the rest falls into place pretty much automatic

verbal steeple
dawn halo
verbal steeple
verbal steeple
#

this has a few names, roblox calls it the explorer, unity calls it the hierarchy. i think unity has a better name for it, but they're both referring to the instance tree

buoyant pumice
verbal steeple
#

there are branches that branch into more branches and leaves at the end

#

all sorts of tree-like language applies, like a root, trunk, branch, leaf, etc

#

it's like your desktop and you have folders and those folders have more folders in them until there's files and whatever at the end

#

this can be arranged into a "tree-like structure", or just simply call it a tree

buoyant pumice
#

yes i understand that now

#

so how does this relate to OOP

verbal steeple
buoyant pumice
#

what is an object property

verbal steeple
#

e.g attributes, intvalue etc

#

like object.MadeOutOfParts= { table_of_parts }

buoyant pumice
#

you mean like if i was to make a sword and the sword has values like "damage, durability"?

verbal steeple
#

when people talk about OOP in roblox scripting, they're usually referring to the latter

#

but you can use the instance tree as a substitute

buoyant pumice
#

so can you give me an implimentation example?

verbal steeple
#

it's still, semantically speaking, an object

buoyant pumice
#

so i can get a better idea of it being used

verbal steeple
#

so are the units on the path

buoyant pumice
#

ahhh

#

so for me i made a config folder in the objects that have "Price", "Damage", "Range," etc. Does that count as OOP?

verbal steeple
#

like the object has a target property, sound property, soundIsPlaying property etc. you could do all of that in pure code, or rely on the instance tree like does the sound instance exist, is the sound instance playing etc. same behavior. different technique to go about it

buoyant pumice
gloomy boneBOT
#

studio** You are now Level 8! **studio

verbal steeple
#

you can do that either with instances like you do there

buoyant pumice
#

so OOP isnt defined to a table?

verbal steeple
#

or you can do it with pure code

buoyant pumice
#

ahhh so i am doing OOP slightly

#

understood

verbal steeple
#

not a strict implementation

buoyant pumice
#

and when is the right time to implement

verbal steeple
buoyant pumice
#

they made it seem like all games were strictly ALL OBJECTS

#

and all code must be reusable

#

etc

verbal steeple
#

it doesn't really matter

#

you can mix and match as you please

buoyant pumice
#

okay thats good

verbal steeple
#

in code, there is never one true correct way to do things

buoyant pumice
#

and what about metatables, because iv heard them ALOT when looking into OOP

verbal steeple
buoyant pumice
verbal steeple
buoyant pumice
#

so why do people use them?

verbal steeple
#

(well.. almost anything, but let's not get bogged down on the nuanced details that don't matter 99% of the time)

verbal steeple
#

some people like em some don't, it doesn't matter

buoyant pumice
#

What makes them so good and useful

verbal steeple
#

i don't think that's relevant to this thread, i also dont want to spend the time writing it down, you're perfectly capable of googling it and learning about it. don't expect to get it in a day.

buoyant pumice
#

I understand, thanks for the help so far, i am gonna dig some research into this tho

verbal steeple