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 5 years ago
Edited 5 years ago
1function generateMedKit(name,XYZ)
2    local MedKits = game.ServerStorage.MedKits.name 
3        MedKits.Position = XYZ                      
4        MedKits.Parent = game.Workspace            
5    end                                            
6generateMedKit("Med1","Vector3.new(463.39, 28.452, 220.52)")
7generateMedKit("Med2","Vector3.new(463.39, 28.452, 208.84)")
8generateMedKit("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.

1function generateMedKit(name,XYZ)
2    local partName = name
3    local MedKits = game.ServerStorage.MedKits.partName
4        MedKits.Position = XYZ                      
5        MedKits.Parent = game.Workspace            
6    end                                            
7generateMedKit("Med1","Vector3.new(463.39, 28.452, 220.52)")
8generateMedKit("Med2","Vector3.new(463.39, 28.452, 208.84)")
9generateMedKit("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 — 5y
0
you want it to find the medkit with the specific name? josestankor 39 — 5y
0
or generate a medkit with the name? josestankor 39 — 5y
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 — 5y

1 answer

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

Use :FindFirstChild(name)

1function generateMedKit(name, XYZ)
2    local MedKits = game.ServerStorage.MedKits:FindFirstChild(name)
3    MedKits.Position = XYZ                      
4        MedKits.Parent = game.Workspace            
5    end                                            
6generateMedKit("Med1","Vector3.new(463.39, 28.452, 220.52)")
7generateMedKit("Med2","Vector3.new(463.39, 28.452, 208.84)")
8generateMedKit("Med3","Vector3.new(463.39, 28.452, 196.33)")

Accept the answer if it helped you!

Ad

Answer this question