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)
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.