I made a script that allows a billboardGUI to open up when I get close to it with Heartbeat function using runservice and checking the magnitude to see if I get close. So when the billboardGUI opens up I am also able to click it, but I also wanted to add something so when the billboardGUI opens when I press the E key it will also do something. But the problem is when I press E it allows me to press E at any location. I want to make it so I can press it ONLY when the GUI opens up.
local Button = game.Players.LocalPlayer.PlayerGui:WaitForChild("Button")
local point = game.Workspace.MainFolder:WaitForChild("Eggs"):WaitForChild("Basic Egg")
local UIS = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local RS = game:GetService("RunService")
RS.Heartbeat:Connect(function()
local players = game.Players:GetPlayers()
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (point.Position - target.HumanoidRootPart.Position).Magnitude
local radius = 5
if distance < radius then
local function handleInput(input)
if input.KeyCode == Enum.KeyCode.E then
print("testing")
end
end
Button.Enabled = true
tweenService:Create(Button, TweenInfo.new(.3, Enum.EasingStyle.Exponential), {Size = UDim2.fromScale(4.5,4.5)}):Play()
else
tweenService:Create(Button, TweenInfo.new(.3, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Size = UDim2.fromScale(.6,.6)}):Play()
task.wait(.01)
Button.Enabled = false
end
end
end
end)
game.Players.LocalPlayer.PlayerGui.Button.TextButton.MouseButton1Click:Connect(function()
print("test")
end)
local function handleInput(input)
if input.KeyCode == Enum.KeyCode.E then
print("testing")
end
end
UIS.InputBegan:Connect(handleInput)
where your code begins and ends for it to format properly. I put the wrong character, sry
local Button = game.Players.LocalPlayer.PlayerGui:WaitForChild(“Button”)
local point = game.Workspace.MainFolder:WaitForChild(“Eggs”):WaitForChild(“Basic Egg”)
local UIS = game:GetService(“UserInputService”)
local tweenService = game:GetService(“TweenService”)
local RS = game:GetService(“RunService”)
RS.Heartbeat:Connect(function()
local players = game.Players:GetPlayers()
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (point.Position - target.HumanoidRootPart.Position).Magnitude
local radius = 5
if distance < radius then
local function handleInput(input)
if input.KeyCode == Enum.KeyCode.E then
print("testing")
end
end
Button.Enabled = true
tweenService:Create(Button, TweenInfo.new(.3, Enum.EasingStyle.Exponential), {Size = UDim2.fromScale(4.5,4.5)}):Play()
else
tweenService:Create(Button, TweenInfo.new(.3, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Size = UDim2.fromScale(.6,.6)}):Play()
task.wait(.01)
Button.Enabled = false
end
end
end
end)
game.Players.LocalPlayer.PlayerGui.Button.TextButton.MouseButton1Click:Connect(function()
print(“test”)
end)
local function handleInput(input)
if input.KeyCode == Enum.KeyCode.E then
print(“testing”)
end
end
UIS.InputBegan:Connect(handleInput)
Looking at the code will take longer, will edit my reply in a bit
Button in the code is a ScreenGui, right? It makes it seem that way with the .Enabled and the .TextLabel, but the Tween tries to get Size of the ScreenGui, which makes my code error.
The main thing though, of wanting to check the distance… you declare the same function twice for getting input, but also distance isn’t checked in the function for getting input, it’s done before, which doesn’t work. I think this fixes the issue:
local Button = game.Players.LocalPlayer.PlayerGui:WaitForChild("Button") -- a ScreenGui?
local TextButton = Button.TextButton
local point = game.Workspace.MainFolder:WaitForChild("Eggs"):WaitForChild("Basic Egg") -- a part?
local UIS = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local RS = game:GetService("RunService")
local isPlayerHoldingDownE = false
local debounceToStopSpam_1 = false
local debounceToStopSpam_2 = false
RS.Heartbeat:Connect(function()
local players = game.Players:GetPlayers()
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (point.Position - target.HumanoidRootPart.Position).Magnitude
local radius = 5
if isPlayerHoldingDownE then
if debounceToStopSpam_1 == false then
debounceToStopSpam_1 = true
debounceToStopSpam_2 = false
print("player is holding down E!")
end
if distance < radius then
Button.Enabled = true
tweenService:Create(TextButton, TweenInfo.new(.3, Enum.EasingStyle.Exponential), {Size = UDim2.fromScale(4.5,4.5)}):Play()
else
print("not close enough!")
tweenService:Create(TextButton, TweenInfo.new(.3, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out), {Size = UDim2.fromScale(.6,.6)}):Play()
task.wait(.01)
Button.Enabled = false
end
else
if debounceToStopSpam_2 == false then
debounceToStopSpam_2 = true
debounceToStopSpam_1 = false
print("player is NOT holding down E!")
end
end
end
end
end)
game.Players.LocalPlayer.PlayerGui.Button.TextButton.MouseButton1Click:Connect(function()
print("test")
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
isPlayerHoldingDownE = true
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
isPlayerHoldingDownE = false
end
end)
There’s probably a better way to check if the player pressed something, but that would then be Event-based and not based on Heartbeat.