How can I regenerate chests without editing each script within it

So I have a little game with a exploration in it and I want to add chests. A lot of them. Maybe even hundereds (def not thousands)
image
The chest has a proximity prompt, which allows to claim it. Here’s how the script looks like so far

local ProximityPromptService = game:GetService("ProximityPromptService")
local name = script.Parent.Parent.Parent.Name
print(name)
-- Detect when prompt is triggered
local function onPromptTriggered(promptObject, player)
    -- collapse the chest because why not
	local Parts = script.Parent.Parent.Parent:GetChildren() 
	for i , v in pairs(Parts) do
		v.Anchored = false
	end
	script.Parent.Enabled = false -- disables the proximity prompt cuz the chest is open
	wait(50)
	script.Parent.Parent.Parent:Destroy()
    --REGENERATION!!!!!!!!!!!!!!!!! (the problem)
	game.ServerStorage.name:Clone().Parent = game.Workspace.chests
end

ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)

The main problem is located at server storage cloning part.

local name = script.Parent.Parent.Parent.Name -- pretty much the chest name aka chest1, chest2, chest3 etc.
game.ServerStorage.name:Clone().Parent = game.Workspace.chests -- <<<<<< the problem line

To save time (and my sanity) I thought I would be able to just input the name into the path where to find the chest in server storage (with it’s proper location) to replace it after 50 seconds so it would look like game.ServerStorage.chest1 Unfortunately it throws a nil error
Workspace.chests.chest1.THEpart.ProximityPrompt.Script:13: attempt to call a nil value

How would I implement chest regeneration by simply changing the model name (chest1, chest2 etc.) and the script wouldn’t require changes in the "problem line"

Instead of inserting a script into each thing, how about you instead use an OOP based system? This will allow you to insert functions into each without having a million of scripts. Having a lot of scripts will greatly hinder your game’s performance.

if you aren’t familiar with it, I can try and walk you through it if you would like. I know this isn’t the exact answer you are looking for, but it will also aid in your script as well.

What is exactly OOP based system? I never heard of that

Object Oriented Programming. Basically, it allows you to create new ‘classes’ as a table. You can then attach functions, connections, and other things to them!

When you do the .name part, it is looking literally for a child called “name”, instead of doing .name, you should be doing [name] instead, which is indexing for the value of the variable called “name”. A more recommended approach than directly indexing like this though, is to use the FindFirstChild function.

It would be good to use a system for this though, as Foursong recommends. There are alternative systems to OOP though, but some way to centralize all your code for the chests would be helpful, rather than having so many copies of it.

2 Likes

Thank you very much! Actually .Name DID a lot of job here. Take a look at the script

-- gets the name of the model. which is set to chest1
local thename = script.Parent.Parent.Parent.Name 
print(thename) -- prints chest1

local item = game.ServerStorage:FindFirstChild(thename) -- somehow finds that chest
item:Clone().Parent = game.Workspace.chests --the chest appears shiny and all new!

Uppercase Name indicated the name in properties. Lowercase name, as you said literally looking for a child

I see this as a more simple solution

1 Like