Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How i can regen multiple models with the same name?

-For my 2D Obby.

When i try to regen the model it only regen one. Im a basic scripter so i dont know what to do :c

model = game.Workspace.piranha
backup = model:clone() 

function onChatted(msg, recipient, speaker) 

local source = string.lower(speaker.Name) 
msg = string.lower(msg) 

if (msg == "regenpiranhas") then
model:remove() 
model = backup:clone() 
model.Parent = game.Workspace 
model:makeJoints() 
end 

end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered)

4 answers

✓ Accepted answer
local names = {'piranha'}
local currentFound = {}
function find(name) --See if it the name is in the table
    for i,v in pairs(names) do
        if name == v:lower() then
            return true
        end
    end
end
function getModelsToRegen(start) --get all of the piranhas.
    start = start or Workspace
    for i,v in pairs(start) do
        currentFound[#currentFound+1] = find(v.Name:lower) and v or nil --Put it in the table if it has the right name.
        getModelsToRegen(v)
    end
end
getModelsToRegen()
for i,v in pairs(currentFound) do
    currentFound[i] = {[1] = v:Clone(), [2] = v} 
end
Game.Players.PlayerAdded:connect(function(Player) --When the Player joins.
    Player.Chatted:connect(function(Message) --When they chat
        if Message:lower() == 'regenpiranhas' then --If they said 'regenpiranhas'
            for i,v in pairs(currentFound) do --Regen the piranhas
                v[2]:Destroy()
                v[1].Parent = Workspace
                v[1]:MakeJoints()
             end
            currentFound = {}
            getModelsToRegen()
            for i,v in pairs(currentFound) do
                currentFound[i] = {[1] = v:Clone(), [2] = v}
            end
        end
end

Untested.

I just was wondering if there was a way to do what i wanted but it looks like there isnt one.

Thanks anyways :) Now i have to multiply it.

Well, an inefficient way of doing it would be to make more variables for the models and copy and paste a lot.

A better way would be to have the names of the models in a table, and loop it with :FindFirstChild ()

You would take the script and multiply it.

Copy and paste is that means, Copy and paste it to how many players there are in the game. So if it is 6 people, you would copy and paste it 6 times.

This discussion is preserved as it was. New answers, comments, and votes are closed.