Need some help with coding tower placement

Hello, recently i’ve been trying to make my own Tower Defense game and i’m struggling with one of the important parts in scripting which is tower placement.
What i’ve been trying to do these past few days is making different types of placement for towers (ground, cliff, water).

Problem is that i don’t know what to do.
I’ve tried GetDescendants(), WaitForChild(), GetChildren() and even following a tutorial made by GnomeCode but i’ve yet to make it work

The picture below is the script that handle tower placeable area and the folder that compose a tower,
the folder “CanBePlaced” is the one i’m having issues with, these are intValues, if the number is 0 then they can’t be placed, if it’s 1 then they can be placed.

If anyone would know how to help me i would greatly appreciate it :smiley:

1 Like
if result.Instance:FindFirstChild("CanBePlaced") then 
  for i,v in ipairs(result.Instance.CanBePlaced:GetChildren()) do 
   if myTower.PlaceType.Value == v.Name then
         CanPlace = true 
  else 
         CanPlace = false
  end
else
     CanBePlace = false
end

i’ll try this and see what it gives me :slight_smile:

Sorry sir, couldn’t make your script not work, though i also should have gaved more details on what each lines means.

towerToSpawn links to the folder of the tower
image
grabModel links to the model (the rig) inside the towerToSpawn folder

result is linked to the mapFolder, with currentPlayedMap linked ot the map been played (here been Baseplate), each type of place for towers are stored in seperate folders inside PlaceablesTowerArea, they are all named Part
image

canPlace is a value inside the script which is true or false depending on which part of the map the mouse is located on

i should i gaved those details sooner, sorry 'bout that

What my code was implying is how you should handle this case, You should have a specific list comparison between the ray result instance and the tower you’re attempting to place by seeing if one of their Surfaces match. For example if your Tower type is “Ground” and in your Part instance you have “Cliff” then this obviously means that you cant place a tower there and you would visually display a effect along the lines of “CANT PLACE HERE”. as for server side checking you would raycast downwards with a whitelist to the map to see if its a valid spot (raycast downwards from the placing position) and then repeat the same as client.

Hm i see, thanks for the help, i’ll see if it’s works and thank your for your time

Is this how i should do it?


image

Why are you using GetChildren() and looping through every existing valid part, You only need to compare with the result Instance.

i am pretty new to scripting, sorry if i’m been annoying to you, just trying to make this script works ;m;

though you gaved me a script with GetChildren() in it

in the snippet i was referring to something that i thought existed, essentially i thought you had had a list in each part called “CanBePlaced” listing only valid placement types, e.g “mountain” part type would only have “Ground” value inside the canbeplaced folder. However this isn’t a big deal since every tds game ive seen never has unique unit types or map types where it would be stuff like for example a Water mountain where a special unit type can sit on that but can also sit on normal cliffs

Here’s a picture of the map, i circled each type of placement, in blue are water placement, in red cliffs and the rest in black is ground

And i didn’t put a value in each part, the values are inside the towers, though now i replaced them with just a simple StringValue named “PlaceType” with the value the name of the part it can be placed on

I’ve manage to make it work ^^
No more issues now.

Thank for the help :smiley:

1 Like