Help With Spawning And Naming Models

Hello my friend is trying to make a system for his game so whenever he clicks a button a new one of that model spawns and he asked me to script it which i thought was easy… then a ran into a problem, the spawning is easy, but when it comes to naming the box it gets out of my range of scripting. Here is my problem whenever you click the button it spawns the model and names it neon, but after the cooldown if you try to spawn it again it does spawn, but it doesn’t appear and is invisible because the script will only recognize the original neon model, so if there was a way i could name the neon boxes for example neon 1 or neon 2 every time a new one is spawned that would be great.

Script
local fff = game.Workspace.BOX:Clone()

script.Parent.ClickDetector.MouseClick:Connect(function(player)

fff.Parent = game.Workspace

fff.Name = "Neon"

game.Workspace.Neon.VehicleSeat.Anchored = false

game.Workspace.Neon.Side1.Anchored = false

game.Workspace.Neon.Side2.Anchored = false

game.Workspace.Neon.Side3.Anchored = false

game.Workspace.Neon.Side4.Anchored = false

game.Workspace.Neon.Bottom.Anchored = false

game.Workspace.Neon.VehicleSeat.Transparency = 0

game.Workspace.Neon.Side1.Transparency = 0

game.Workspace.Neon.Side2.Transparency = 0

game.Workspace.Neon.Side3.Transparency = 0

game.Workspace.Neon.Side4.Transparency = 0

game.Workspace.Neon.Bottom.Transparency = 0

game.Workspace.Neon.VehicleSeat.CanCollide = true

game.Workspace.Neon.Side1.CanCollide = true

game.Workspace.Neon.Side2.CanCollide = true

game.Workspace.Neon.Side3.CanCollide = true

game.Workspace.Neon.Side4.CanCollide = true

game.Workspace.Neon.Bottom.CanCollide = true

script.Disabled = true

wait(10)

script.Disabled = false

end)
1 Like

Well, you can try putting a variable inside the connected function that goes up 1 every time it is called, then concatenate that with the name of the box, like so:

local counter = 1 --Makes variable before function
Script.parent.MouseClick:Connect (function (Player)
counter = counter + 1
--stuff
fff.Name = "Neon"..counter

and that should work. Hope I helped! :smile:

3 Likes

Hey thanks , but how would i apply the neon + counter to this for example

game.Workspace.Neon.Side1.Anchored = false

Well, reference that fff variable you made for the neon box, instead of referencing it from the workspace.

Hello! :wave:
Your script currently has a couple major flaws that prevent it from functioning, let’s find and resolve them!

Issue 1: Model access
You don’t need to give your model a completely new name access it. Instead, keep using your variable fff to access your model through the entire script, replacing game.Workspace.Neon. On another important note, clone your model inside of your function.

Issue 2: Debounce
Currently your method for debouncing (adding a cooldown) is by disabling the script and then enabling it again. The issue with this approach is that the script is disabling itself, not letting it enable itself later on. How can we fix this? Let’s use variables instead!

local debounce = false

function Run () 
    if debounce == false then 
        debounce = true 
        -- Run code here

        wait(1) -- Cooldown Time
        debounce = false
    end
end

In the example above, we have created a variable called debounce. Each time we run the function, check if this variable is set to false and if it is, run the code. Inside the code we begin by setting the value of debounce to true, blocking new threads from being ran. Once we have ran our code, wait for a couple of seconds (This is the cooldown time) and then set debounce to false once again.

Tidying up
Currently you are writing the same three properties (Anchored, Transparency and CanCollide) to your parts, this gets messy really quickly. Let’s utilize a loop! Loops can run through all of the parts inside your model and write these three properties, it even supports new additions to your model!

for _, v in pairs(fff:GetChildren()) do
      v.Anchored = false
      v.Transparency = 0
      v.CanCollide = true
end

In the for loop above we go through all the children inside of the model fff (labeled v) and write properties to them. You can visualize v as all your parts in your model.

Results
Let’s put this into action!

local debounce = false

function Run (Player)
    if debounce == false then 
        debounce = true -- Block new threads

        local fff = game.Workspace.BOX:Clone() -- Our clone

        for _, v in pairs(fff:GetChildren()) do -- Loop through fff's parts and write the following values:
            v.Anchored = false
            v.Transparency = 0
            v.CanCollide = true
        end

        fff.Name = "Neon" 
        fff.Parent = workspace 

        wait(10) -- Our cooldown
        debounce = false -- Let us access the function again
    end
end 

script.Parent.ClickDetector.MouseClick:Connect(Run) --Each time we click the part, run our function.

Good luck!

1 Like

WOW THANKS SO MUCH!!! This does help me with my script and tidiness and such. I will also learn from this.