I have some code that I made inside of a key model as well as a billboard gui inside of the key whenever your mouse hovers over the key’s hitbox.
I want to check for the player’s input when their keyboard presses e but I want it to also check if the billboard gui is enabled.
I have no idea how I can do this because whenever I put if statements after if statements the code breaks, so I know I can’t do it like that.
I’m thinking of doing something like if gui == enabled and --player presses e then keytool.Parent = player.Backpack end
here are the variables:
key = script.Parent
hitbox = key.Hitbox
detector = hitbox.ClickDetector
highlighter = key.Highlight --not necessary because there is already code for it
gui = hitbox.GrabInterface
also the code is in a normal script inside of a union called “Key”
hopefully people respond because it took a while to make this post
Note
whenever the player’s mouse hovers over the key’s hitbox it enables the gui and whenever the mouse leaves the key’s hitbox it turns disables the billboard gui
UserInputService is definitely the way to go when checking if a player pressed a key and such. You would use input.KeyCode to detect what key the player pressed. and then just simply create the if statement.
local Game = game
local UserInputService = Game:GetService("UserInputService")
local BillboardGui = nil --Reference to 'BillboardGui' instance.
local function OnInputBegan(InputObject, GameProcessed)
if GameProcessed then return end
if InputObject.KeyCode.Name == "E" then
if BillboardGui.Enabled then
--Equip tool code.
end
end
end
UserInputService.InputBegan:Connect(OnInputBegan)
local Part = workspace:WaitForChild("Part") -- The Part for the GUI's Parent
local root = script.Parent:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, gpe)
if input.KeyCode.Name == "E" and not gpe then
if (Part.Position - root.Position).Magnitude <= 20 and Part:FindFirstChild("BillboardGui") then
local clone = game:GetService("ReplicatedStorage"):WaitForChild("Tool"):Clone()
clone.Parent = root.Parent
end
end
end)
It needs to be in startercharacterscripts.
Change the Part to the part that has the BillboardGui In it.
Change 20 to the Billboard guis maxdistance, so that we can get an accurate point for the player to be to get the tool.
You can also use ContextActionService to do this. It’s better to use this as there are conditions you might not want your player to pick the item up in and if so, it’s a lot easier to use this than spamming if statements
Fair enough, I’ve not played around with it much myself tbh. I’ve just seen it and it’s just easier in case you ever want specific conditions in the future
You’ll need to use a ‘RemoteEvent’ object since input can only be detected on the client and the ‘ServerStorage’ container’s contents do not replicate to the client.
key = script.Parent
hitbox = key.Hitbox
detector = hitbox.ClickDetector
highlighter = key.Highlight
keytool = game.ServerStorage.Key
gui = hitbox.GrabInterface
local uis = game:GetService("UserInputService")
local function highlight()
highlighter.Enabled = true
gui.Enabled = true
end
local function unhighlight()
highlighter.Enabled = false
gui.Enabled = false
end
uis.InputBegan:Connect(function(input)
if input.KeyCode.Name == "E" then
if gui.Enabled then
print("yes yes")
end
end
end)
detector.MouseHoverEnter:Connect(highlight)
detector.MouseHoverLeave:Connect(unhighlight)