Store Health of Rocks

im trying to make a game similar to refinery caves and oaklands

i need to store the health and max health of each rock

my initial plan was to use humanoids or number values in each rock but that seems like a bad idea because there will be hundreds of rocks

is there a more efficient way to do this or is that fine

you can store them in a table:

RocksHP = { 
    Rock1 = 100,
    Rock2 = 50
}

Use NumberValues. They don’t use much memory or anything like that and can be accessed by different scripts unlike tables.

Yes, pretty bad idea.
I like @MikeartsRBLX Idea of using tables. And you can use Attributes on each rock too, which is more efficient than values

Values consumes more memory than attributes, and any script can access a table if its a module

1 Like

Yeah, actually attributes would be best in this situation. You could group rocks with the same MaxHealth together into a folder/model giving the model a MaxHealth attribute that all of its children would possess, then each rock would have a health attribute showing their indiviual health. That’s assuming MaxHealth is consistent and the most important property of the rocks.

1 Like

The children wouldn’t have the MaxHeath attribute, but their parent model could be accessed for ascertaining a particular rock’s MaxHealth.

I think it highly depends on the mining mechanics. If its desired that each rock has its own health property and the rock preserve that health forever, then no.
If a group of rocks share the same attribute which belongs to a parent model, then its not possible to have individual health per rock.
If the idea is just having a health limit that will go back to full if the player stop mining it, (idk like minecraft, if you stop mining the block returns to its full health) then yes. Having a base health that will be checked and reduced each hit and when it reach the max limit it will break, would be efficient to have “groups” of rocks.

In the case thats the desired behavior, and there are multiple types of rocks, and its health goes back to full if player stop the mining. A folder with attributes or even a table with folder’s name (rock type) to know the max rock health in order to break it

1 Like

I didn’t see this reply of yours, but basically what I wrote here was what you had already suggested but assuming that an ore instance has a constant max health shared with other ores of the same kind, therefore we should group similar ores into folders. If you have diamond ore the max health may be 100 while gold’s max health is 60. In this case, we can put similar ores and rocks into folders based off of how long it would take each to be mined(Max Health). For example, cobblestone ore models would go into a folder called cobblestones, and the folder would have an attribute called MaxHealth set to 20. This way we don’t need to have a MaxHealth attribute for every stone, only a Health attribute, as Health will be the only property different for any ore at any given moment.

Tables within a module script would work great as well but I prefer using properties, attributes, and BaseValues for systems in scenarios where they be just as modular and performant.

But as you said the OP does need to clarify further how the system works because we’re currently working off of assumptions.

1 Like

Also, everything I said in this comment was wrong—don’t know what I was thinking :blush:

1 Like

I think this is key, yeah!
We could imagine thousands of ways that a mining system should work, depends on the desired behavior

1 Like

Add an attribute to all different rock types called health. Folder these rocks under a folder called “AllTypesOfRocks”. Every time you make a new rock generate on the map, clone a rock under the folder, then parent to somewhere else. Attributes will carry on when cloned. To remove health, you can use SetAttribute. When the value is less than 0, destroy the rock.

  1. Create a Rock Template: In Roblox Studio, create a template for your rocks. This can be a Model containing a Part representing the rock and other necessary parts such as a HealthBar.
  2. Add Properties: Add NumberValue instances to your Rock template to store the health and max health values.
  3. Scripting: Write scripts to handle the logic of your rocks. You can use the Instance’s properties to retrieve and update health values.
local rock = script.Parent -- Assuming the script is a child of the rock object
local healthValue = rock:WaitForChild("Health")
local maxHealthValue = rock:WaitForChild("MaxHealth")

-- Function to damage the rock
local function damageRock(amount)
    local currentHealth = healthValue.Value
    local newHealth = currentHealth - amount
    if newHealth <= 0 then
        -- Rock destroyed
        rock:Destroy()
    else
        -- Update health value
        healthValue.Value = newHealth
    end
end

-- Example usage
local damageAmount = 20
damageRock(damageAmount)
  • rock represents the rock object.
  • healthValue and maxHealthValue are NumberValue instances storing the health and max health values, respectively.
  • The damageRock function decreases the rock’s health by a specified amount and destroys the rock if its health reaches zero.

This approach allows you to manage the health and max health of rocks efficiently within the Roblox environment, similar to what you might have seen in games like Refinery Caves and Oaklands.

are you ai generated
you seem rather ai generated

i am just gonna store max health for every ore type in a module script and then create a health value after the rock is damaged

should be more efficient than having a table of every rock in existence

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.