how do I make a buttonGUI that when clicked shows a GUI? - lua

I would like this to be a buttonGUI or a proximity prompt (proximity prompt preferred)

You haven't given much context of the file structure, so I can only do my best to help.
So first you need to add a event listener to the button. So as a child of the button add a localscript and in that script add the listener.
local GUI_Holder = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local UI = GUI_Holder.Your_UI_To_Activate -- Path to the GUI element
script.Parent.MouseButton1Click:Connect(function()
UI.Enabled = true
end)
Lines one and two look pretty bad however you can replace then with you own way of fetching the UI. (https://developer.roblox.com/en-us/api-reference/class/TextButton)
If you need any more help reply to this :)
-- Harvey

Harvey has a good answer but I recommend using button.Activated
local GUI_Holder = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local UI = GUI_Holder.Your_UI_To_Activate -- Path to the GUI element
script.Parent.Activated:Connect(function()
UI.Enabled = true
end)

Related

If a player doesn't own a gamepass, then prompt it via ScreenGUI not working?

I'm trying to make a localscript for a TextButton so that when somebody clicks on it, the script checks if they have the gamepass. If they do, it will do a multitude of other things. But, if the player doesn't have the gamepass, then it will prompt it to them.
Here is the code for my localscript:
local ID = 42076468
local plr = game.Players.LocalPlayer
local MPS = game:GetService("MarketplaceService")
script.Parent.MouseButton1Click:Connect(function()
if MPS:UserOwnsGamePassAsync(plr.UserId,ID) then
-- do stuff
else
MPS:PromptGamePassPurchase(plr.UserId,ID)
end
end)
The problem is that it doesn't prompt the player, even when I remove my conditional statements. This localscript is inside of a TextButton, inside of a Frame, inside of a ScreenGUI.
Any help is deeply appreciated, thanks.
You've made a simple mistake of calling PromptGamePassPurchase(player, gamePassId) the same way as UserOwnsGamePassAsync(userId, gamePassid).
PromptGamePassPurchase expects you to pass in a Player as the first argument, whereas UserOwnsGamePassAsync expects a userId. So all you gotta do is correct the first argument.
MPS:PromptGamePassPurchase(plr, ID)
Also, don't forget to define the callback for the ProcessReceipt event!

LuaU script (Roblox), how can I make a Key get pressed with a script

An example would have been like
local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E)
but it doesnt work ofcourse because i cant jsut make my game press E by itself with this thing, so it requieres soemthing longer and if you find the solution can you also do one where it makes it pressed down?
The input can only be registered on the client, therefore you will have to code in a LocalScript. There are 2 services which are used to get the player's input:-
UserInputService
This example shows how to use the UserInputService in getting the player's LeftMouseButton input.
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed!")
end
end
UserInputService.InputBegan:Connect(onInputBegan)
ContextActionService
This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function ()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
You should take a look at the Wiki pages.
It sounds like you want to write a script to press the E key, but that isn't possible.
You can bind actions to keypresses, just like in the examples Giant427 provided, and you can also manually call the functions that are bound to those actions, but you cannot write a script to fire keyboard inputs.

ROBLOX Studio function to call function for each door to open

I'm pretty new to ROBLOX development and don't really know how to make the objects talk just yet.
I have 16 cells all with an individual open and close button. Those work. I want to create a button that will open all doors at once.
function onClicked()
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.CanCollide= false
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
The above function is on each cell and works. I would like to loop through each one and fire it when I click a different button. I've been looking into getting each object with the same names but haven't been able to iterate through it.
The below code is my attempt to get it to fire off each one!
local part = workspace.OpenButton
local clickDetector = Instance.new("ClickDetector")
local function onMouseClick(player)
for _, child in pairs(workspace.PrisonCell:GetChildren()) do
print(child)
child:connect(child.Door.onClicked)
end
end
clickDetector.Parent = part
part.Parent = workspace
clickDetector.MouseClick:connect(onMouseClick)
Any help with this would be greatly appreciated!
You could do something similar to this to open all of the prison doors at once:
local part = workspace.OpenButton
local clickDetector = Instance.new("ClickDetector")
local function onMouseClick(player)
for _, child in pairs(workspace.PrisonCell:GetChildren()) do
child.Door.Transparency = 1
child.Door.CanCollide = false
end
end
clickDetector.Parent = part
part.Parent = workspace
clickDetector.MouseClick:connect(onMouseClick)
The downside to using the solution above is that if you want to change the door opening script, you would have to change it in both the individual cell buttons as well as this master button script. If you think this might be a problem in the future, I would consider writing a master script somewhere in ServerScriptService that uses BindableEvents for communcation with the button clicking scripts in order to keep the door opening function in one place.

How can My GUI appear when my noob is killed?

[My code is :]
local function MWin()
game.StarterGui.ScreenGui1.DemonWin.Visible = true
if game.Workspace.Mages_Boss.Humanoid.Died:connect(function()
print("good")
end
[My noob is named : Mages_Boss
And my screen gui is named : DemonWin
I dont know what to put for "print("good")".]
First, modifying the starter GUI does nothing. You need to change the one player or all players with a for loop. In my answer, I’ll use the former with a player named ROBLOX. If you want to start with it invisible, you need game.Players.ROBLOX.PlayerGui.ScreenGui1.DemonWin.Visible = false. To make it visible on the death event, use game.Players.ROBLOX.PlayerGui.ScreenGui1.DemonWin.Visible = true.
Try using
DemonWin.Enabled=true;
or
DemonWin.Enabled=false;
to toggle whether it is active or not. I suppose in the died function, use the latter.
I'm going to assume you're not firing the function so you can get rid of that. You're also going to remove the .Died since it fires even when it's not dead. So your best bet would be to do also add a debounce- kind of function to your script. Here's the modified version:
game.StarterGui.ScreenGui1.DemonWin.Visible = false
if game.Workspace.Mages_Boss.Humanoid.Health == 0 then
game.StarterGui.ScreenGui1.DemonWin.Visible = true
else
game.StarterGui.ScreenGui1.DemonWin.Visible = false
end
Also, you would have to put the GUI in the StarterGui (located in game.Players.LocalPlayer.StarterGui) in order to not publicly malfunction this script.

GMOD Lua Use type doesn't matter?

recently I have been creating an imitation HarborRP gamemode for Garry's Mod, and I'm trying to recreate the Smuggler NPC (You'll know what I mean if you've ever played HarborRP) So basically I want the NPC to open ONE Derma-Frame window when the player presses their use key on it. I have the NPC created and all of that, but when the player simply presses their use key on the NPC, a million windows pop up, I have the NPC/Entity's use type set to SIMPLE_USE, but it seems like that doesn't matter because so many windows pop up. the VGUI/Derma Frame's settings is set to MakePopup() but that doesn't matter either. See if you can find whats wrong with it, I have very little knowledge of LUA.
init.lua file:
include("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
util.AddNetworkString("smug")
hook.Add("PlayerUse", "menuu", function(ply, ent)
net.Start("smug")
net.Send(ply)
end)
function ENT:Initialize()
self:SetModel('models/humans/group01/female_01.mdl')
self:SetHullType(HULL_HUMAN)
self:SetHullSizeNormal()
self:SetNPCState(NPC_STATE_SCRIPT)
self:SetSolid(SOLID_BBOX)
self:SetUseType(SIMPLE_USE)
self:DropToFloor()
end
cl_init.lua file:
include("shared.lua")
function ENT:Draw()
self.Entity:DrawModel()
end
net.Receive("smug", function()
if( !frame ) then
local frame = vgui.Create("DFrame")
frame:SetSize(900,600)
frame:SetPos(ScrW()/2-450,ScrH()/2-300)
frame:SetVisible(true)
frame:MakePopup()
frame:SetDeleteOnClose(true)
elseif (frame) then print("HI")
end
end)
shared.lua file:
ENT.Base = "base_ai"
ENT.Type = "ai"
ENT.PrintName = "[DEV] Smuggler NPC"
ENT.Category = "InVaLiD's HBRP Entities"
ENT.Author = "InVaLiD"
ENT.Spawnable = true
ENT.AdminSpawnable = true
ENT.AutomaticFrameAdvance = true
Something To Note
All of these files are in the addons/smug_npc/lua/entities/ folder.
Yes, I know I have weird names for things, that's just me.
I have a basic to no knowledge of lua, so explain things please :)
I really do appreciate your help, and your will to help people, please know that I thank you all for being here just to sort other people's problems even though you could be spending your time doing something productive, Thank you!
You need to put your code that does net.Send into the entities ENT:Use function rather than on the PlayerUse hook.
function ENT:Use(ply)
net.Start("smug")
net.Send(ply)
end
The below line which you already have in your code in the initialize function makes the entity call the ENT:Use function once when a player presses E on it, which it seems that you want to do so that is good:
self:SetUseType(SIMPLE_USE)
Also, I recommend checking out the gmod forums if you need developer help in future.
GMod forums: https://gmod.facepunch.com/f

Resources