I’m developing a tycoon-style game and I’m almost finished, I just need to implement this system. The game is about game development. I need to create a system that detects when the player has a desk, on which a keyboard, a monitor, and a PC are placed, and then creates a ‘setup’, as this setup will be used to generate income
However, I’m having trouble figuring out where to start to detect these items. I would like to find the best way to make it work effectively.
It sounds like you want to have players assemble PC setups by bringing together the right components.
One way you can do this is by giving the player keyboards, monitors and PCs as Tools that they can carry around. In the area where you would like to have a computer setup, place an invisible hitbox part around it and use a BasePart.Touched event to listen for a tool touching it. Once a tool that is being carried by a player touches it, remove the tool from the character and place the appropriate component in the setup.
A sample script that achieves the above might look something like this:
local owner : Player -- assumed to be defined
local setup : Model -- assumed to be defined
local hitbox : BasePart = setup:FindFirstChild("hitbox")
hitbox.Touched:Connect(function(hitPart : BasePart)
local character = owner.Character
if character and hitPart.Parent:IsA("Tool") and hitPart:IsDescendantOf(character) then
local tool : Tool = hitPart.Parent
if tool.Name == "PC" or tool.Name == "Keyboard" or tool.Name == "Monitor" then
-- make sure the component isn't already added to the setup
if setup:FindFirstChild(tool.Name) == nil then
local newPart : BasePart = hitPart:Clone()
-- the tool part is usually named handle, so lets rename it to the tool name
newPart.Name = tool.Name
newPart.Anchored = true
newPart.Parent = setup
-- TODO: position newPart correctly on the setup.
-- remove the tool from the character
tool:Destroy()
end
end
end
end)
Elsewhere you might want to have a script that polls your setups and gives you money every 10 seconds if the setup is properly completed:
-- Assumes all setup models are in a special folder
local setupFolder : Folder -- assumed to be defined
for _, setup : Model in setupsFolder:GetChildren() do
if setup:FindFirstChild("PC") and setup:FindFirstChild("Keyboard") and setup:FindFirstChild("Monitor") then
-- TODO: award the player money for this setup
end
end
If you want to save player progress, you will need to figure out a way to serialize the tycoon state and then apply it again when the player rejoins. One way to do this by giving each setup location a unique name and using a table to determine which setups are already filled. e.x.:
Alternatively if you have different types of components that can be in place, you can assign them names or IDs and store those instead of the true and false values. When the player loads in, you can read their data and copy the components over to each setup appropriately.
I don’t think this would work for what I’m aiming for. It was my mistake for not explaining it better. Each setup has to be created separately because the player will be able to open the setup and interact with the game they are developing to earn more money. So, that setup has to be saved individually, I think.
In this case the solution will depend heavily on the building system you are using. Does the building system already include a way to determine when objects are placed on other objects? You can listen to this event and add a tag to the desk or “setup base” object. Otherwise you may need to listen the object added signal and calculate it manually using raycasting or similar.