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

WaitForChild of a variable?

Asked by 5 years ago

Hi, I wanted to take the Button's name as a variable when that button is clicked on. And by doing so, I plugged in the variable into the WaitForChild, hoping it would detect the button's name and find that in replicated storage. This doesn't seem to be working, any ideas?

This is an attempt of a 3dmodule onto a UI

SelectedPlane = nil
Options = script.Parent.Parent.Parent.group:GetChildren()
for i=1,#Options do
    if Options[i]:IsA("ImageButton") then
        Options[i].MouseButton1Click:connect(function()
            local Button = Options[i]
            SelectedPlane = Button.Name
        end)
    end
end

local Frame = script.Parent:WaitForChild("screen")
local Module3D = require(game.ReplicatedStorage:WaitForChild("Module3D"))
local Towers = game.ReplicatedStorage:WaitForChild(SelectedPlane)

local Model3D = Module3D:Attach3D(Frame,Towers)
Model3D:SetDepthMultiplier(1.2)
Model3D.Camera.FieldOfView = 5
Model3D:SetActive(true)

game:GetService("RunService").RenderStepped:Connect(function()
    Model3D:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
end)
0
Ok, so just to clarify, you have a image button, that when pressed, you want to then be able to find an object with a corresponding name parented directly under replicated storage? SerpentineKing 3885 — 5y
0
@SerpentineKing yes sp33dmcreation 2 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Alright, so assuming the relevant lines of code you have a problem with are:

SelectedPlane = nil
Options = script.Parent.Parent.Parent.group:GetChildren()
for i=1,#Options do
    if Options[i]:IsA("ImageButton") then
        Options[i].MouseButton1Click:connect(function()
            local Button = Options[i]
            SelectedPlane = Button.Name
        end)
    end
end

Revised Script

local SelectedPlane = nil
local group = script.Parent.Parent.Parent:WaitForChild("group")

for i, v in pairs(group:GetChildren()) do
    if v:IsA("ImageButton") then
        v.Activated:Connect(function()
            SelectedPlane = v.Name
        end)
    end
end

repeat wait() until SelectedPlane ~= nil

SCRIPT STRUCTURE

You can use in pairs instead of "Options[i]" and using the "local Button = Options[i]"

connect is considered deprecated so capitalize it to Connect

Use Activated rather than MouseButton1Click if you want more devices than just a PC to properly play your game.

DIRECT ISSUES

When you define "Towers" in your above script, you are using "SelectedPlane" while it is still considered nil since the script is running through all lines of code before you can activate the function. To circumvent this, you can add a repeat loop to wait until the "SelectedPlane" is not nil then allow the script to move on.

NOTE

The above script can only be used once based on how it is set-up, so if you need to repeat the "SelectedPlane" cycle again, you will need to disable and re-enable the script.

0
Ok, so basically just simplified the script and made it more efficient. But the "repeat loop" repeats what? when I plug this in along with your revised codes, nothing seems to happen. The code below the repeat line does not get read. sp33dmcreation 2 — 5y
0
The repeat is to prevent the selected plane from remaining "nil" since it is called before the "v.Activated" can be. This essentially means that with your initial function, the "Towers" was always considered nil, regardless of the button pressed. SerpentineKing 3885 — 5y
0
yeah I understand that now, but since the v.Activated:connect doesn't activate, it is stuck on that repeat loop. sp33dmcreation 2 — 5y
0
As in the ImageButton is not activating? Have you tested a print inside the function to check if that's the issue? Are there any errors in the "Output" window? SerpentineKing 3885 — 5y
View all comments (2 more)
0
ImageButton is not activating, I've put a print line under the activate function. It does not get printed. no errors in output. everything is where they are supposed to be and they are an imagebutton. sp33dmcreation 2 — 5y
0
can you place "print(i ,v)" under the for-do section to check if the table is nil SerpentineKing 3885 — 5y
Ad

Answer this question