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

How can I select different part names within a function?

Asked by 3 years ago
Edited 3 years ago
function generateMedKit(name,XYZ) 
    local MedKits = game.ServerStorage.MedKits.name  
        MedKits.Position = XYZ                       
        MedKits.Parent = game.Workspace             
    end                                             
generateMedKit("Med1","Vector3.new(463.39, 28.452, 220.52)")
generateMedKit("Med2","Vector3.new(463.39, 28.452, 208.84)")
generateMedKit("Med3","Vector3.new(463.39, 28.452, 196.33)")

I understand what is not working here, but I am not sure how to fix it. The variable is being set to "game.ServerStorage.MedKits.name", but I need the script to make "name" whatever I write within the argument like "Med1" that way for each function I call I can select a different part within the ServerStorage.

function generateMedKit(name,XYZ) 
    local partName = name
    local MedKits = game.ServerStorage.MedKits.partName
        MedKits.Position = XYZ                       
        MedKits.Parent = game.Workspace             
    end                                             
generateMedKit("Med1","Vector3.new(463.39, 28.452, 220.52)")
generateMedKit("Med2","Vector3.new(463.39, 28.452, 208.84)")
generateMedKit("Med3","Vector3.new(463.39, 28.452, 196.33)")

I also tried doing it this way trying make "partName" a variable of the "name" I wanted, but it didn't work.

Sorry if it is hard to understand what I'm asking, I just started scripting and can't explain this stuff well yet. I'm not even sure if any of this script could even work.

1
Edit the post so the code is within a code block rexhawk 222 — 3y
0
you want it to find the medkit with the specific name? josestankor 39 — 3y
0
or generate a medkit with the name? josestankor 39 — 3y
0
I have a group called MedKits with Med1 through Med3 within it and I need to select one Med at a time. So yes I am trying to select a specific Med ElectricZooo 55 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Use :FindFirstChild(name)

function generateMedKit(name, XYZ) 
    local MedKits = game.ServerStorage.MedKits:FindFirstChild(name)
    MedKits.Position = XYZ                       
        MedKits.Parent = game.Workspace             
    end                                             
generateMedKit("Med1","Vector3.new(463.39, 28.452, 220.52)")
generateMedKit("Med2","Vector3.new(463.39, 28.452, 208.84)")
generateMedKit("Med3","Vector3.new(463.39, 28.452, 196.33)")

Accept the answer if it helped you!

Ad

Answer this question