Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 01:55 PM |
I'm trying to get a script to target a child on the Character of a Player via the :findFirstChild() method but would like it to target any child with a specified text in it, instead of just the exact name.
Example: I am trying to get the script to target a gun inside the Character named "Pistol"; I know that to do that I could use :findFirstChild(Pistol") The problem - I would like it also to target any child with the text "Pistol" inside of it, such as if a gun was inside the Player and it's name was "Standard Pistol" I can create a :findFirstChild() for each and every gun but I would rather have one method to call all children with "Pistol" in their name.
If that doesn't make sense, or any of you need more detail on it, I would be glad to supply it. Thank you, Chao. |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 01:58 PM |
String manipulation. I'm not sure how to use many of them, but this one will work.
for i,v in pairs(Character:GetChildren()) do if string.sub(v.Name,(string.len(v.Name)-6) == "Pistol" then --do things break end end
I'm pretty sure you have to use a loop. |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 01:59 PM |
for _,v in pairs(Character:GetChildren()) do if string.find(v.Name,"Pistol") then --Do whatever you need to do here. The variable 'v' serves as your target (in this loop, at least; v is local to this loop) end end |
|
|
| Report Abuse |
|
|
Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 02:00 PM |
Thanks, I'll try it now. It's for a ClipGui that resides in the player's PlayerGui and the script is in fact operated inside of a while true do loop. |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:00 PM |
| Use his. Mine will only work if "Pistol" is the last six letters of the name. |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:00 PM |
@killjoy
That's assuming the word pistol is at the end of the object's name. I think for his purpose the word Pistol can be anywhere in the object's name. |
|
|
| Report Abuse |
|
|
Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 02:05 PM |
Puzzle, you are correct. I am trying to get it to target any child with "Pistol" in it's name
Also, this is how I've used it and it doesn't seem to be working:
local char = script.Parent.Parent.Parent.Parent.Parent.Character -- This does define the character
for _,v in pairs(char:GetChildren()) do if string.find(v.Name,"Pistol") then local gun = string.find(v.Name,"Pistol") end end |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:09 PM |
change it to this:
local char = script.Parent.Parent.Parent.Parent.Parent.Character -- This does define the character
for _,v in pairs(char:GetChildren()) do if string.find(v.Name,"Pistol") then local gun = v end end |
|
|
| Report Abuse |
|
|
Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 02:12 PM |
| Already tried that, doesn't work either. Would you like the entire script to see what I am trying to do? |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:18 PM |
That's because string.find returns number values of the specific start and end characters that match.
http://wiki.roblox.com/index.php/Function_Dump/String_Manipulation#string.find |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:18 PM |
Woops, one more thing:
for _,v in pairs(char:GetChildren()) do if string.find(v.Name,"Pistol") then local gun = v break end end
Do you get an error? Or does it just not work? |
|
|
| Report Abuse |
|
|
Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 02:19 PM |
| Ah, I see. Well, is there any other way for it to target a child named "Pistol" with any further prefix/suffix to it's name such as "Pistol 1" or "1 Pistol" and then target said pistol's Children? |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:19 PM |
| Why does no one listen to me? |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:19 PM |
@killjoy
That's true, but what I did works perfectly fine. |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:20 PM |
That last post was aimed at Puzzle
try using string.match (it's on the same page) |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:21 PM |
| string.find is not the problem. It might have something to do with gun being local inside the loop |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:21 PM |
| Oh, I see, so the numbers wouldn't even matter, just if it actually finds it, then it's true. Sorry. |
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:22 PM |
Try this:
local gun = nil
for _,v in pairs(char:GetChildren()) do if string.find(v.Name,"Pistol") then gun = v break end end
if gun ~= nil then -- do stuff here end |
|
|
| Report Abuse |
|
|
Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 02:23 PM |
@Kill - Your script doesn't work either. Here's the entire stupid thing: ---------------------------------------------------------------------------------------------
repeat wait() until game.Players.LocalPlayer.TeamColor ~= nil
if Game.Players.LocalPlayer.TeamColor ~= Game.Teams:findFirstChild("Visitors").TeamColor then script.Parent.Parent.Visible = true else script.Parent.Parent.Parent:Remove() end
while true do wait() local char = script.Parent.Parent.Parent.Parent.Parent.Character --It practically just ignores this part here for _,v in pairs(char:GetChildren()) do if string.find(v.Name,"Pistol") then local gun = v break end end -- Yet if it's named exactly any of the below things it works local gun2 = script.Parent.Parent.Parent.Parent.Parent.Character:findFirstChild("Machine Gun") local gun3 = script.Parent.Parent.Parent.Parent.Parent.Character:findFirstChild("Sniper") local gun4 = script.Parent.Parent.Parent.Parent.Parent.Character:findFirstChild("Rifle") local gun5 = script.Parent.Parent.Parent.Parent.Parent.Character:findFirstChild("Shotgun") local gun6 = script.Parent.Parent.Parent.Parent.Parent.Character:findFirstChild("Assasin's Pistol") local gun7 = script.Parent.Parent.Parent.Parent.Parent.Character:findFirstChild("Sniper Rifle Of Chaos") -- This just labels it's ammo value if gun ~= nil then script.Parent.Text = gun.Props.Ammo.Value.."/"..gun.Props.ClipSize.Value elseif gun2 ~= nil then script.Parent.Text = gun2.Props.Ammo.Value.."/"..gun2.Props.ClipSize.Value elseif gun3 ~= nil then script.Parent.Text = gun3.Props.Ammo.Value.."/"..gun3.Props.ClipSize.Value elseif gun4 ~= nil then script.Parent.Text = gun4.Props.Ammo.Value.."/"..gun4.Props.ClipSize.Value elseif gun5 ~= nil then script.Parent.Text = gun5.Props.Ammo.Value.."/"..gun5.Props.ClipSize.Value elseif gun6 ~= nil then script.Parent.Text = gun6.Props.Ammo.Value.."/"..gun6.Props.ClipSize.Value elseif gun7 ~= nil then script.Parent.Text = gun7.Props.Ammo.Value.."/"..gun7.Props.ClipSize.Value else script.Parent.Text = "0/0" end end
|
|
|
| Report Abuse |
|
|
|
| 28 Apr 2013 02:24 PM |
@Chao
Replace that loop with what I posted. |
|
|
| Report Abuse |
|
|
Chao4163
|
  |
| Joined: 10 Oct 2011 |
| Total Posts: 39 |
|
|
| 28 Apr 2013 02:27 PM |
| Thank you SO much. I'm going to be having similar names of SO many guns and needed this so badly. It worked that time. Thank you both for all the help, Chao. |
|
|
| Report Abuse |
|
|
| |
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:29 PM |
Since string.find returns the number values, does that also mean it returns true? because otherwise you might want to do if string.find(v.Name, "Pistol") ~= false then |
|
|
| Report Abuse |
|
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 28 Apr 2013 02:29 PM |
| Never mind, this is why I never tried learning string manipulation. I always sucked at it. |
|
|
| Report Abuse |
|
|