Local Script inside PlayerGui:

local Input = game:GetService("UserInputService")
local MyServerEvent = game.Workspace.MyServerEvent
local SelectedItem

function onKeyPress(Key)
    Key = Key.KeyCode
    if Key == Enum.KeyCode.E then
    local Player = game.Players.LocalPlayer
    local gui = Player.PlayerGui.Pickup.Frame
    local itemname = SelectedItem.Item.Value
    local items = game.ReplicatedStorage.Items
    local item = items:findFirstChild(itemname):Clone()
    local character = Player.Character
    local distance = (character:findFirstChild("Head").Position - SelectedItem.Position).magnitude
    
        if distance <= 10 then
                if character.Humanoid.Health > 0 then
                    if gui.Visible == true then
                        MyServerEvent:FireServer(item, SelectedItem)
                        gui.Visible = false
                        print("item picked up")
                    end
                end
            end
    end
end


game["Run Service"].RenderStepped:Connect(function() 
     for i, item in ipairs(game.Workspace.Items:GetChildren()) do
     SelectedItem = item
    item.ClickDetector.MouseHoverEnter:connect(function(Player)
         Input.InputBegan:Connect(onKeyPress)
    end)
end 
end)

Server Script inside ServerScriptService:

local event = Instance.new("RemoteEvent")
event.Name = "MyServerEvent" 
event.Parent = game.Workspace

event.OnServerEvent:connect(function(Player, Item, SelectedItem)
        Item.Parent = Player.Backpack
        SelectedItem:Destroy()
end)

The issues are:

• Sometimes I am able to pick up, at other times I am not able to pick anything up - With no errors.

• At other times there is an error saying line 10: Item is not a valid member of Part (Local Script).

• The scripts do not work in Multiplayer mode, only in Solo mode.

• Sometimes the weapon I intend to pick up, is not the weapon I receive in my tool bar - because any weapon at the range of 10 studs from my character is picked up.

1 comment

0

Constant event binding on line 29-36 will definitly get in your way. Use ChildAdded and ChildRemoved.