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

How do you search for children using string values?

Asked by 5 years ago

I have this bit of code, i'm trying to find the child of a part with the same name as a string value. How do I fix this?

local gui = LocalPlayer.PlayerGui:FindFirstChild('UI2')
    local ArmorName = gui.Frame.Armors.EquipScript.armorname.Value
local armors = game.ServerStorage.Armors:GetChildren()

    for i = 1, #armors do
        if armors[i].Name == ArmorName then
        --do stuff
        end
0
is this a server script or local script theking48989987 2147 — 5y
0
if it's a local script,you cant access the ServerStorage theking48989987 2147 — 5y
0
if it's a server script, you can't access a localplayer theking48989987 2147 — 5y
0
It's a server script in server storage, and don't worry, the localplayer is called from a remote event so that definitely is not a problem. UnderWorldOnline 59 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

I got a similiar code from someone when I asked a question here at Scripting Helpers and now I just changed it so it fits for your needs. Hope this helps!

for i, v in pairs(armors) do
    if v.Name == ArmorName then
        --do stuff
    end
end
0
This is good but it really depends on its use. If you just want to find the armor name and make it do stuff you don't have to write a table for this. voidofdeathfire 148 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I'm merely repeating the comments but anyways. Localscripts are scripts that are meant for the client/user. Serverscripts are, you guessed it, meant for the server. Both scripts have access limitations in them, for example, LocalPlayer does not exist in a serverscript while ServerStorage contents are inaccessible in a localscript.

A simple counter to your situation is to use remotes.

A localscript will gather information from the GUI and pass it as an argument to a remote stored in ReplicatedStorage.

The remote will have two parameters, the client who invoked the remote (automatically provided; "client") and the name of the armor ("ArmorName").

Run this when it is invoked:

ArmorModel = game.ServerStorage.Armors:FindFirstChild(ArmorName)
-- Do Stuff
1
If there is only 1 armor with that name then just do game.ServerStorage.Armors[ArmorName] BlackOrange3343 2676 — 5y
0
Whoops, I'll fix that. YTRaulByte 389 — 5y

Answer this question