Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my keybind button not working for all the buttons in the workspace folder I made?

For some reason only one button part prints out "works" when i press E near it. Here's a gif that might give a better idea of what I mean by only one button working: https://gyazo.com/166db5021bad5bee22ac264a08cc4b2b

And this is the workplace folder I mean't: https://gyazo.com/71e1ef4f99be3bad03fb75a8c39b3951

The code is below.

local runservice = game:GetService("RunService")
local buttons = game:GetService("Workspace"):WaitForChild("Buttons")
local guibutton = script.Parent.Button
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local cam = game:GetService("Workspace").CurrentCamera
local UIS = game:GetService("UserInputService")
local canpressbutton

runservice.RenderStepped:Connect(function()
    guibutton.Enabled = false
    for _,button in pairs (buttons:GetChildren()) do
        local m = (button.Position - char.HumanoidRootPart.Position).Magnitude
        local maxDist = 8
        if m <= 8 then
            local ScreenPoint = cam:WorldToScreenPoint(button.Position)
            guibutton.Enabled = true
            guibutton.Button.Position = UDim2.new(0, ScreenPoint.X, 0, ScreenPoint.Y)
			canpressbutton = true
		else
			canpressbutton = false
        end
    end
end)

UIS.InputBegan:Connect(function(i)
	if i.KeyCode == Enum.KeyCode.E and canpressbutton then
		print("works")
	end
end)

5 comments

0

you havent defined the e key

0

...

0

Tested it out, Change :GetChildren with :GetDescendants()

0

still wont work, mixgingengerina10

0

Works for me, let me make some tweaks then.

1 answer

Thanks to FloweryMonkeyboy5 from the scripting helpers discord, I solved it. The thing to fix it was to add a simple return to the if statement. Big thanks to him. The resulting code looks like this:

local runservice = game:GetService("RunService")
local buttons = game:GetService("Workspace"):WaitForChild("Buttons")
local guibutton = script.Parent.Button
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local cam = game:GetService("Workspace").CurrentCamera
local UIS = game:GetService("UserInputService")
local canpressbutton

runservice.RenderStepped:Connect(function()
    guibutton.Enabled = false
    for _,button in pairs (buttons:GetChildren()) do
        local m = (button.Position - char.HumanoidRootPart.Position).Magnitude
        local maxDist = 8
        if m <= 8 then
            local ScreenPoint = cam:WorldToScreenPoint(button.Position)
            guibutton.Enabled = true
            guibutton.Button.Position = UDim2.new(0, ScreenPoint.X, 0, ScreenPoint.Y)
            canpressbutton = true
		return
        else
            canpressbutton = false
        end
    end
end)

UIS.InputBegan:Connect(function(i)
    if i.KeyCode == Enum.KeyCode.E and canpressbutton then
        print("works")
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.