Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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)
0
you havent defined the e key Blondeguy2008 0 — 4y
0
... MrDarkLord1234 7 — 4y
0
Tested it out, Change :GetChildren with :GetDescendants() mixgingengerina10 223 — 4y
0
still wont work, mixgingengerina10 MrDarkLord1234 7 — 4y
0
Works for me, let me make some tweaks then. mixgingengerina10 223 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
Ad

Answer this question