Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can one script be used for more than one part?

Asked by 5 years ago
Edited 5 years ago

I am trying to find a way to have more than one part/model being connected to one script.I tried to name more than one part/model the same thing: "Sword" so the one script can react to the same name.

Here is the script I used:

local Clicker = script.Parent
local Object = game.Lighting:WaitForChild("Sword")
ObjectSpawn = game.Workspace:FindFirstChild("ObjectSpawn")

Clicker.ClickDetector.MouseClick:Connect(function(player)
    Object:Clone().Parent = game.Workspace
end)

Do I do need to do something different?

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Edit: I misunderstood the question, OP wanted to pick a random model and clone it to the workspace.

Remember to put your models in either a folder named Models or a group named Models:

clicker = script.Parent

clicker.ClickDetector.MouseClick:Connect(function(player)
    local models = game.Lighting.Models:GetChildren()

    --get a random model by picking a random number in the range of 1 through the number of models inside the models group/folder: 
    local randomModel = models[math.random(1, #models)]

    --clone model and put it into workspace: 
    local modelClone = randomModel:Clone()
    modelClone.Parent = game.Workspace
end



Old post:

this should work if you want to make the click events for a bunch of parts but don't want to create the events individually

You can do all this in one script by using loops: Put all your clickers and your objects in a table, then you can have the loop create click events for the items in the clickers table.

--TODO: put references to all your clickers in the clickers table below
--I put some values in there as examples
--clickers = {
    workspace.Clicker1, workspace.Clicker2, workspace.Clicker3
}

--TODO put all your objects for your clickers in the objects table below
--I put some values in there as examples
objects = {
    game.Lighting.Object1,
    game.Lighting.Object2, 
    game.Lighting.Object3
}


--loop through clickers and create click events for them
for i = 1, i < #objects do
    clickers[i].ClickDetector.MouseClick:Connect(function(player)
        objects[i]:Clone().Parent = game.Workspace
    end)
end

Ad

Answer this question