Hey, so I've been messing around trying to work on making a gui pop up with these tools when you're within 10 studs but I get no error just nothing appears

local tools = {["SWORD"] = true, ["AXE"] = true}
local radius = 10
local removeToolUponPickUp = true -- true and the tool is removed when picked up, false and it stays

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()


local running = false
local found, PressE

--variables ^


function getTools()
    local found = {}
    for _, c in pairs(game.Workspace:GetChildren()) do
        if tools[c.Name] then
            table.insert(found, c)
        end
    end
    return found
end

--creates table for tools from Tools model in Lighting


function walking(speed)
    running = speed > 0 and true or false
    while running and not found do
        for _, tool in pairs(getTools()) do
            local pos = tool:GetModelCFrame().p
            if player:DistanceFromCharacter(pos) < radius then
                found = true
                local PressE = Instance.new("ScreenGui")
                local TextLabel = Instance.new("TextLabel")
                PressE.Name = "Press E"
                PressE.Parent = game.StarterGui
                TextLabel.Parent = PressE
                TextLabel.BackgroundColor3 = Color3.new(1, 1, 1)
                TextLabel.BackgroundTransparency = 1
                TextLabel.Position = UDim2.new(0.336745143, 0, 0.892933607, 0)
                TextLabel.Size = UDim2.new(0, 318, 0, 50)
                TextLabel.Font = Enum.Font.Fantasy
                TextLabel.Text = "Press E to pick up "..tool.Name
                TextLabel.TextColor3 = Color3.new(0.760784, 0.741176, 0.67451)
                TextLabel.TextSize = 24
                TextLabel.TextStrokeColor3 = Color3.new(0.219608, 0.219608, 0.219608)
                found = tool
                while true do
                    if player:DistanceFromCharacter(pos) > radius then
                        PressE:Destroy()
                        found = nil
                        break
                    end
                    wait(0.25)
                end
            end
        end
        wait(0.25)
    end
end




mouse.KeyDown:connect(function(key)
    if key:lower() == "e" and found then
        local tool = game.Lighting.Tools[found.Name]:Clone()
        tool.Parent = player.Backpack
        if removeToolUponPickUp then
            found:Destroy()
            if PressE then
                PressE:Destroy()
            end
        end
        found = nil
    end
end)

--Takes tool from lighting and duplicates into player starterpack, then deletes model

repeat wait() until player.Character
wait(0.5)
player.Character.Humanoid.Running:connect(walking)

2 comments

0

Try using .magnitude to find the distance between two objects.

0

MouseKeyDown is deprecated.