E to equip tool

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

Screen Shot 2022-09-18 at 9.59.44 AM

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

Looks like you already have the basics down.

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.

1 Like

Alright, will try this. Thanks! If it works I would give you a solution but I will test it later because I have to go do some yard work

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)
1 Like

there is already a billboard gui variable but ok

is there a way to refer to the player? so I can move the tool from serverstorage to player.backpack

this is what the code looks like:

uis.InputBegan:Connect(function(input)
	if input.KeyCode.Name == "E" then
		if gui.Enabled then
			print("yes yes")
		end
	else
	end
end)

but it doesn’t work

and noone replies back );This text will be blurred

1 Like

You can try something like this:

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.

Replace tool with the actual tool.

1 Like

the else breaks the code… that’s why

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

1 Like

There’s only 2 if statements?

But I can understand the use of Context Action Srve, but this is simpler, and It still works.

1 Like

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

2 Likes

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.

2 Likes

oh, i forgot to say that it didnt even work anyways hehe

Can you provide your script(s) and a screenshot of your instance hierarchy so I can see where you went wrong?

1 Like

like this?

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)

This is a local script right? Where is it located?

1 Like

it is a normal script inside of a meshpart named key

The ‘UserInputService’ will only work in local scripts (input can only be detected by the client).

1 Like