#touch event makes one enemy makes me lose x18 times

1 messages · Page 1 of 1 (latest)

uncut sandal
#

i was making a tower defense game, the base got 3 hp (each went down by 1 if an enemy touches the base) when one enemy walks in my base and triggers the touched event 18 times please help

#

i think i posted way too much of these help posts..

pastel solstice
#

put some cooldown on it

#

or make a table that saves the enemy and check to make sure that the same enemy doesnt hit twice

uncut sandal
#

ill just demonstrate rq

#

wait wha i added task.wait for the touched event kept executing continuously

pastel solstice
#

just putting task.wait doesnt stop the event from firing again

#

you need debounce

uncut sandal
#

here

maiden ledgeBOT
#

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

uncut sandal
#

i put the timer to 10 to uhh

#

yk

#

imagine if multitple enemies folded into one place doesnt it just do the bug inside the video

peak heart
#

you're applying your debounce too broadly

#

you have it so that if an enemy touches the brick 10 seconds have to pass before another enemy touching it is valid

#

you can use a table to track whether an enemy has touched the brick before and that could be your debounce

cinder hare
# uncut sandal

so for my game you can pick up balls and to make sure players dont fire the OnTouch multiple times i use debounce with a key

local key = player.UserId .. "-" .. trashType
  if not debounce[key] then -- if debounce isnt set  
    debounce[key] = true -- set debounce
    local PlayerData = PlayerStore.GetData(player) -- get playerdata
    local TrashAmount = PlayerData.trash -- get trash
    if TrashAmount >= PlayerData.maxCapacity then -- if their trash is at max
      debounce[key] = nil -- Clear debounce immediately if at capacity
    return
  end
#

the key has their userID since its unique to the player and then trashType which has 3 different types

#

this method has worked well for me you could try that, that way it stores the object and player so it wont fire more then once

uncut sandal
cinder hare
uncut sandal
#

im confused is it just mean that if debounce == false or debounce == nil??

uncut sandal
#

how can i do it

peak heart
#

yeah just think a little bit more abstract about debounce

#

create a table

#

when an enemy touches the brick, check if the enemy is found inside the table

#

if not insert them in the table and continue

#

that way the code will only run once for each enemy

uncut sandal
#

i just asked chatgpt is it what you mean?

cinder hare
cinder hare
# uncut sandal i just asked chatgpt is it what you mean?

This looks good the idea is

If you just added a debounce with a timer then timing it with the enemies would be hard

So instead you do a debounce but with specific enemies
So if EnemyA touching your part he can not fire it again

But EnemyB is able to immedialty

How you do that is how you have
Stick them in a table if they have
And add a check

uncut sandal
uncut sandal
#

i lowk just copied the internet and see that people just not do if the enemies touches the base, they just do a bindable when enemies waypoint value = nil it will just send the value to the base

#

no more :touched

#

(i dont understand a single of that script)