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

How to create buttons from scripts and set their values?

Asked by 3 years ago

Hello Friends. Thank you in advance for your support.

I am planning to create a "trivial pursuit"-like game on Roblox. I have noticed that from from any script I can create a "part" using the "instance.new" call.

The part will create, and I can set the size, position etc in real time, from any script anywhere. However, I want to create Textbuttons from a script (or localscript) and modify its attributes from anywhere too, but only works if the script parent is the button itself.

As an example, I would like to create a textbutton for each player in a game. I need to count the players first, and then create as many buttons as players exist. However I cannot make the buttons appear in screen. Here is the code I use to create the buttons from a script in inside "ScreenGui":

local boton = Instance.new("TextButton",game.StarterGui.ScreenGui)

boton.Name="testing1"

boton.AnchorPoint = Vector2.new(0.5, 0.5)

boton.Position = UDim2.new(0.5, 0, 0.5, 0)

boton.Visible=true

boton.Size=UDim2.new(0,200,0,100)

boton.Text="Testing"

print (boton.Text)

The boton Textbutton creates itself, it set the proper parameters to it, but you can see nothing on the game screen. Is like the button is hidden or ... just doesnt show anything.

Do you know what am I doing wrong? (note that the print output in the last line prints "Testing")

Thank you again.

0
Please put your code inside of code blocks. Cynical_Innovation 595 — 3y
0
Place the button inside of PlayerGui, not StarterGui. StarterGui will replicate to PlayerGui when the player joins/resets. So do “game.Players.LocalPlayer.PlayerGui.ScreenGui” when you’re parenting the TextButton. AntiWorldliness 868 — 3y

3 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Is your code in a server script or a LocalScript if its in a LocalScript, then your problem may be that you're parenting a textbutton to the starter gui when you need to be parenting it to the player gui ex:

local player = game.Players.LocalPlayer
local sgui = player.PlayerGui:FindFirstChild("ScreenGui",true)
if sgui then
--code here
end
0
Good my friend. This worked. I placed this in a localscript in game.startergui.screengui and worked. Thank you so much zergvantex 2 — 3y
0
Very welcome. switchflake 24 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Ok let me start off by saying you can Instance Gui's and put them inside the screen gui and change their properties, but they will not appear, you will see them in the explorer, yet they will not be visible, I am not sure why it is like this but, what you actually want to do is design the gui in studio and use a script or local script to change the visible property of the gui to true, for example I create a textbutton in studio and put it inside the screenGui and set visible to false,Then I can make a shop button with a local script to make the other textbutton visible, For example:

--Local Script(Client)
script.Parent.MouseButton1Click:Connect(function() --event for when button is clicked

local screenGui=script.Parent.Parent  --reference to screenGui
local boton=screenGui.boton2 --reference to the button you want to make visible
if boton.Visible==false then --if the button isn't already visible
script.Parent.Text="Close Shop"  --indicates to player to close shop with the same button
boton.Visible=true  
elseif boton.Visble==false then
script.Parent.Text="Open Shop" --indicates to player that clicking will open shop
boton.Visible=true
end
end)

Now you might use that on a frame that manages your actual shop gui. Also if you want to show a gui from the server you can, even though I don't see why you would, you can use something like this and put it in ServerScriptService:

game.Players.PlayerAdded:Connect(function(player)  -- event to detect when player is added
--with an argument to detect the player that was added
local personalGui=player:WaitForChild("PlayerGui",10):WaitForChild("ScreenGui",10)
--reference to the player's gui
personalGui.WelcomeMessage.Visible=true   --the gui you want to display becomes visible
wait(10)  --the script is idle for 10 seconds
personalGui.WelcomeMessage.Visible=false  --the gui you want to choose becomes invisible
--you can also destroy the gui like this
personalGui.WelcomeMessage:Destroy()   --built-in function to destroy gui
end)
Log in to vote
0
Answered by 3 years ago

Amazing guys! thank you so much. Going to try it right away. By the way, do you know any comprehensive, holistic roblox-scripting course?

Thank you for your amazing help.

Answer this question