Hello, im trying to make a building system like that of theme park tycoon 2, resturaunt tycoon 2, and zoo tycoon 2. I read the tutorial by EgoMoose, but i still am unsure of to proceed with something like this.
How would i create a build mode type of thing?
How do i add the ability to delete placed objects?
How do i add the ability to select which object will be placed?
i have tried and failed to do this on my own, so any help is appreciated. I DO NOT need any complete scripts, but i do need tips and explanations on how i would go about said build system.
Here are some tips, you might need to wander around in old posts for scripts, but you basically need to know this :
To create the “build mode type of thing”, you might want to add a ScreenGui in your StarterGui with all the components necessary for building (the items to place, tools, grid sizing among others). You can then place a localscript in the ScreenGui which will handle the visiblity of it. For example, when you start the game, the scripts sets the visibility (Enabled property) of the ScreenGui to false, but each time that you press a key (like Q), you can toggle its visibility (gui.Enabled = not(gui.Enabled))
Once you have that, you just need to make the building system around it and make it functional only if the gui is Enabled or not
To delete placed objects, you need 2 things : make sure the deletion tool is active (for example, pressing a textButton in your building ScreenGui) and always check what the user is hovering with his mouse. I’m pretty sure you can do that with RayCasting and easily get the hovered part, and from there, it should be fairly easy if you want to highlight it in red (and not highlight it when the user is not hovering it anymore) and if you want to delete it when the user clicks on his screen.
Lastly, to select which object should be placed, I would recommend having finished my first bullet point with the placement gui, and make sure that the user can access all of the building items from it (add a scrolling frame for example with every item in it). Make sure you add an invisible textButton covering the whole area of each building item and listen for the .Activated event of each textButton (with a for loop). When a textButton is activated, you can easily get the item the user wants to place (the parent of the button) and for example have somewhere an instance of class ObjectValue and set its value to the selected part.
You can then get the value from the ObjectValue and it will change each and every time that the user selects an item from your scrolling frame.
I hope this helps, please let us know if you have more questions!
You mean the for loop to get which building item was selected ? I got you !
Basically, you would write something like this :
for i, child in scrollingframe:GetChildren() do
child["PATH TO BUTTON"].Activated:Connect(function()
-- what happens
end)
end
The for loop requires either an iterable item (like it could be a variable you defined earlier or a temporary one) which would look like for i=1,10 do ... end, here i is created and deleted after the loop and goes from 1 to 10. It can also require a table, which is the case here as something:GetChildren() will return an indexed table (keys are ordered numeric values like 1,2,3,4,…) with the children in the something.
Hope this helps !
PS : I have heard somewhere that when you use tables (in for loops for example), you should not put ipairs() or pairs() because it reduces the efficiency. In a for loop, not putting ipairs()/pairs() also work !