#enemies touches base once and script works but the second enemy script no workk

1 messages · Page 1 of 1 (latest)

granite warren
#

i was making a tower defense game, i created a script for my base, but when the second enemies (as shown in the video) did not execute the .Touched?? and destroy print base hp??

delicate rivetBOT
#

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

granite warren
#

enemies will be cloned from serverstorage to game.workspace.mobingame(folder) if you need this info

#

and maybe id be very thankful if you know how to fix the first npc moved into the abse and the game stops for like 0.25s

#

no i would be very thankful

spare crow
#

Hello

#

Ik why

#

First of all, when u assign the variable hmob u are waiting to assign only 1 instance (so one clone)

#

(And not all the clones)

#

So same thing for variable mob, btw idk why u used hmob, since u didnt used it in ur script

#

You want that when u clone a mob from the server storage, its goes to the mobingame folder, and THEN, you will check all the mobs in that folder

#

To check all the mobs in that folder, the easiest way would be first to convert it to a table, an array of all mobs inside the folders

#

To do that u can do : lua workspace.mobingame:GetChildren() you can now here have the table of all the mobs inside ur folder

#

GetChildren gives u all the children of the folder or a instance, and makes it as a table (an array precisely)

#

So when something is touching, you need to check and get the table of all rigs, and in this table, go and see for key, value in this table if the value is equal to the ennemy, if ye then u code ur things

#

U want me do the code or u wanna do it urself?

#

Idk if i explained well or even if its an optimal way but thats working if im not wrong

#

Btw if u need more explanation dont hesitate

granite warren
#

wait so to sum up what i do wrong is i only check a single mob not all of the folder itself?

#

i tried getchildren but it just end up destroying all the enemies if only one touches the base

#

how do i call the children individualy inside the getchildren's table?

granite warren
#

i think something is wrong with line "if enemy == mob" or "local mob" but i dont know what is wrong

granite warren
#

oh yeah i think i fixed it but enemies just spam the you lose promt instead of doing it once for eaach enemy but ill make it in a different channel

spare crow
#

Thats the main and only reason i can see

spare crow
spare crow
#

When u do get childrens at the start

#

It do it immediatly

granite warren
#

so what should i do

spare crow
#

What should you do is

#

Whne the base is touched

#

You will crrate a variable

#

That will get all the children of the folder

#

Then

#

You will do a for loop

#

Inside the table

#

To check if the ennemy is equal to one of the instances in the folder

#

So one of the mobs

#

So u need to local mobs when the base its touched

#

U understood or no?

#

If u still dont i can make the script for u

granite warren
#

i think you should make a script for me cause i lowkey dont understand it

#

but i kinda fixed the problem being mobs cant destroy but i kept warning me it cant find do something but the script works

#

ill reocrd rq

spare crow
granite warren
spare crow
#

Its just cuz u can get better if u do it urself, and try, if u cant ill give u the script

granite warren
#

while waiting for u to respond i asked chat gpt and used it in my script

spare crow
#

Ah

granite warren
#

it do works but it kepting warning me about something

#

i dont know how to fix

spare crow
#

Ok u want me to do the script?

granite warren
#

and mobs spamming you lose once it touches the base

spare crow
#

Copy paste me ur script if yes

granite warren
#

im trying to log into studio it kept kicking me out

spare crow
#

Why?

granite warren
#

i got in

spare crow
#

So u want me to do the scriptv

#

?

granite warren
#

yeah lowkey

#

just to see what my script is missing

#

heres the version i asked chat gpt

#

the error and touch spamming

spare crow
#

Copy paste me the script pls @granite warren

delicate rivetBOT
#

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

granite warren
#

oh yeah mb

#

local base = script.Parent
local basehp = 3
local hmob = game.Workspace.mobingame:WaitForChild("Rig"):WaitForChild("Humanoid") -- enemies will be cloned to mobingame folder
local mob = game.Workspace.mobingame:GetChildren()
local mobfolder = game.Workspace.mobingame

base.Touched:Connect(function(enemytouchedbase)
local enemy = enemytouchedbase.Parent and enemytouchedbase

if enemy:IsDescendantOf(mobfolder)  then-- if enemy = the children inside the mobingame folder then do this

    enemy:Destroy()
    print("enemy has touched base")
    basehp = basehp - 1
    print("the base health is now:", basehp)

    
    if basehp <= 0 then
            print("you lose")
    end
end

end)

spare crow
#

Im on mobile so it will be a bit hard but i should be able i wait

granite warren
#

im going to sleep rn so you can have as much time writing your script as you want

#

ill respond to ur messages when u wake up

#

bye g

spare crow
#
local base = script.Parent
local basehp = 3

base.Touched:Connect(function(partThatIsTouching)
    local enemy = partThatIsTouching.Parent
    local mobs = game.Workspace.mobingame:GetChildren()
    for _, mob in ipairs(mobs) do
        if enemy == mob then
            mob:Destroy()
            print("enemy has touched base")
            basehp = basehp - 1
            print("the base health is now:", basehp)
        end
    end
    if basehp <= 0 then
        print("you lose")
    end
end)
#

should be working

granite warren
#

Ill check it when im at home thabks for the sceipt