#How to make multiple functions run multiple times at once
1 messages · Page 1 of 1 (latest)
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
task.spawn
how do u create new tower objects?
make sure you are creating new tower objects and not referencing the same one
this one is infinitely playing an sfx sound when it shouldnt
wait let me get the script for that
i dont have that key on my kb can you send it me for copypaste
if you dont have ` in your keyboard hold alt+9+6 in that order
uhhhh
i got an error for trying to send it as text 😭
ill seperate it into 2 parts
POV: you dont have nitro 
-- 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
at this point just send a .txt file with the code XD
what is a .txt file going to do to my computer? 
and ctrl+v to paste it
i know these im not stupid
but ctrl a will give you 1000 lines of code
even after looking myself i cant see the problem in the code at all
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
iv heard of task.delay what does it do
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
and for task.spawn, would i do it like "task.spawn(tower.CheckAttack)(newTower, player)
nah
task.spawn(function()
— your script here
end)
this
do it
bye
ima go sleep
i dont think the issue has anything to do with the task scheduler

what do you think it is
in my opinion it has to with something that should be unique to each tower but instead is a reference to the same object for every tower
but with this little code i cant really proove or deny it xd
how would i go around making them fully unique to each other
i dont really know what is not being unique, i know the newTower thingy is unique because its a roblox instance, the most sus thing is that tower.function() object
** You are now Level 12! **
the biggest suspect is that entire module
whats up with it?
wait
this might help
all the towers attacked individually and worked until i changed something in these 2 blocks of code:
yall going in loops here
wdym?
show the firing side of this event
yep this one
animateTowerEvent:FireAllClients(newTower, "Attack", target)
i assume CheckAttack is called for every tower
once a tower is added to the game, it calls it
does the enemy take bonus damage if you have a tower very far away?
if it does then it is in fact attacking and your range check is broken
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
so break down the problem, couple of things you can know with this
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
second off, if the sound effect is stacking many times, then we can also infer you are creating multiple connections to something either on the firing side or on the receiving side (e.g tower is calling CheckAttack twice in one frame when it should only be once)
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
yes, but the sound is also playing from both towers when 1 is attacking
i suspect you are cloning scripts. where is this script located
and where is this one
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
doesn't seem like any duplicates there so far
the issue has to be on the animation side, because the only problems im having are with the animations
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.
iv tried this already, thats how i got here
and how many times does it fire
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
slow down
you have too many prints here i think, i dont know what the others are, only 'Found Target in Animation'
okay let me remove some
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
okay 1 sec
** You are now Level 7! **
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
ya you only need that one print, disable the others and try again
maybe add teh tower to it
print("Sound is playing", tower)
ohhh
wait
im stupid
wrong function
1 sec
taht was checking for queen attack not pawn
okay
with 1 tower:
you should also print the tower, use Tower:GetDebugId()
with 2 tower:
to see if it is one or both
seems like with 1 tower it is recognising that the sound is playing
but with 2 its not
that works too
add the identifier to this so we can see which tower it is playing the sound for
right i forgor, that is only for plugins T_T
just use tower:GetFullName()
and hopefully the 2 towers have different names
lol
with 2 towers:
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)
tower.Name = tower.Name .. math.random(etc
but that has a chance to give them the same identifier
so use time() instead idk lol
it's unlikely enough to not matter
this is debug only
ill do math.rand for now and then sort it later
so just before i clone it?
no after.
😭
after you clone it change the clone.Name bruh
i assume nothing else relies on tower.Name
if you do, then give it an attribute with the id 
ya good enough just remember to remove it later. now run test
hopefully only one tower shows up
if both show up then we have identified the problem
well we were wrong
let me print the newtowers name right after i give it the name
WAIITTTT
where's the number wot
i may have found the problem with the creation
why is it the same tower that's strange
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
😭
aw
no dude that wasn't to fix it, that was just to help identify the problem
i know just making sure i relay all information
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
but i want both to play the sound, but only when that specific tower attacks
instead of only when 1 attacks
just run the test with one tower attacking and the other not.
y'know, your fail condition at the moment
show the output..
oh shit my bad
don't need a blow-by-blow
seems like it's not coming from the attack then
yes
you can include it in the print
like print(a,b) use a comma, not ..
print(tower.Name,"did a thing")
ya i noticed you're working with things you dont understand
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?
no its happening somewhere else
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
and i doubt you can afford to hire anyone
yep add a print with the name next to every single one
that's the only 2 :Play in the whole codebase?
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
honestly i'd only need about 5 minutes with your project and this would probably be over
not really
fair play
besides i'd probably lock you out of your own project because age checks
why, your acc underage or something?
no, yours is
???
bro im in my 30s 😭
add prints next to sound:play()
alright
it's a fairly safe assumption 90% of the time
being roblox and all
but ya debugging is a whole entire skill set
and the only sound plays related to it are in animations
writing code is more reading code than writing it.
other than that there are 3 more that look for audio.ErrorSound
completely unrelated
so its just the 2 :Play's
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
true
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
anyways with the new prints its still only playing the pawnsound
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
don't just guess or assume
well there's your answer
but it only clones too many when 2 towers
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
ahh okay
so how do i go around making it run on both
im so shit with client stuff
if the second pawn was attacking, there'd be sounds in there.
but ya just keep coming up with things to test, and then actually test it.
im confused
there's also this fact...
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
ahhh
you have to prove literally everything
im aware
this is an educated guess but it's still a guess
so how do we check if thats what is actually happening
just like you did before
add debug code to your code to prove things
prints go a long way
e.g looks like the sound is playing every half second
mind you, i have no idea what this sound is, how long it is, or anything else like that
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
but the sound needs to be looped, thats how the effect works
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
never said you could
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
Pyro-Fire == best scripter ever
turns out the shared reference was the debounce
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
well i didnt know this was an option
bro im so lost

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
you chose a tower defense game, you need to be able to work with stateful objects to make those
aka learn more OOP
iv tried, but i cant wrap my head around it
anyways, ignoring OOP, how do you clone a script per tower???
lol if the sound is playing then it's a safe bet that the tower 1- has a sound object somewhere in it (probably wherever you cloned it to), and 2- that sound object .IsPlaying==true
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
which is fine for beginner, but if you're going to write on that level you should learn to be more familiar with your instance hierarchy and reading it: this will work, it's not a great solution, oop is better for the game you're making, but you can still make a tower defense game relying exclusively on the instance tree instead of proper code objects
since the instance tree is a viable substitute for proper code objects
Whats the best way to learn OOP in your opinion, and also would you be able to give me a starting walkthrough, i watched like 14 different videos and literally get lost at meta tables
just think of the instance tree as if it were an object
whats an instance tree?
and the rest falls into place pretty much automatic
root (model/folder/any instance)
|- head
|- rootpart
|- sound
|- body
|- humanoid
|- animator
etc
making things typesafe in a normal language: 
making things typesafe in luau: 
don't get me started on the typesolver
im still lost
the way folders are arranged in the explorer
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
i undeerstand
because it's structured like a tree
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
you can treat a root instance like an object, and all of its child parts as object properties
what is an object property
anything that has a value
e.g attributes, intvalue etc
like object.MadeOutOfParts= { table_of_parts }
you mean like if i was to make a sword and the sword has values like "damage, durability"?
sure, you could have an attribute for damage and durability, or you can write it in pure code like mySword = {} mySword.Damage=67 mySword.CharacterOwner = player.Character mySword.ToolInstance = player.Backpack:FindFirstChild("MySword") and so on
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
so can you give me an implimentation example?
it's still, semantically speaking, an object
so i can get a better idea of it being used
ya your towers are objects
so are the units on the path
ahhh
so for me i made a config folder in the objects that have "Price", "Damage", "Range," etc. Does that count as OOP?
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
** You are now Level 8! **
yes exactly
you can do that either with instances like you do there
so OOP isnt defined to a table?
or you can do it with pure code
no OOP is a concept
not a strict implementation
and when is the right time to implement
it doesn't matter?
cause when i learnt about it online i saw it as this
they made it seem like all games were strictly ALL OBJECTS
and all code must be reusable
etc
yeah i know some people make everything an object
it doesn't really matter
you can mix and match as you please
okay thats good
in code, there is never one true correct way to do things
and what about metatables, because iv heard them ALOT when looking into OOP
again, OOP is a concept not a specific technique. metatables are a useful tool in lua, and is a very good way of doing pure code objects, but the concept of an object doesn't change just because you used a particular technique or method to do it. a tower in your defense game is still a tower no matter how you implement it.
okay i understand, so why are metatables so discussed, what is the major benefit of them compared to traditional code
anything that can be done with metatables can be done without them.
so why do people use them?
(well.. almost anything, but let's not get bogged down on the nuanced details that don't matter 99% of the time)
because it's a useful and convenient tool that has a lot of desirable properties
some people like em some don't, it doesn't matter
what are those convenient tools?
What makes them so good and useful
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.
I understand, thanks for the help so far, i am gonna dig some research into this tho
if you ever get stuck on something you can always open a new thread here #1020374354867007528
-# results may vary