local player = game.Players.LocalPlayer local mouse = player:GetMouse() local target = mouse.Target local tools = { --add tools here from wherever you store them. They are copied and put into --the players inventory.. also, keep em in REPstorage. game.ReplicatedStorage.Item1:clone(); game.ReplicatedStorage.Item2:clone(); game.ReplicatedStorage.Item3:clone(); game.ReplicatedStorage.Item4:clone(); } local acceptedItems = { "Item1"; "Item2"; "Item3"; "Item4"; } game:GetService('UserInputService').InputBegan:connect(function(input,proc) if not proc then if input.UserInputType == Enum.UserInputType.Keyboard then if input.KeyCode == Enum.KeyCode.Q then if target.Name == acceptedItems[] then --compares strings, ask SH what to put in []'s print("accepted item detected, taking it now :D") target:Destroy() if acceptedItems.Name == tools[] then tools:Clone() --above singles out a single tool from list tools.Parent = player.Backpack end end end end end end)
This is actually a request by robloxiveboy , he had to get off right when I finished what I could do. I used to have a game that incorporated this to the next level, but it got deleted along with my old account.
I'm having trouble with singling out a single value from the table in the pastebin code and using that to give said player the correct item (tool in this case)
This is a typical problem when first learning Tables. You're trying to access a singular element, when the keys to those elements are all integers, and thus you'd have to loop through at least one of the Tables to get the matching element.
To avoid that, we can combine the two Tables in your code into one.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local tools = { Item1 = game.ReplicatedStorage.Item1; --No need to Clone here since we Clone them later, below. Item2 = game.ReplicatedStorage.Item2; Item3 = game.ReplicatedStorage.Item3; Item4 = game.ReplicatedStorage.Item4; } game:GetService('UserInputService').InputBegan:connect(function(input,proc) if proc then return end --This is a useful shortcut for Event-connected functions, to avoid the typical Lua indentation levels. if input.UserInputType ~= Enum.UserInputType.Keyboard then return end if input.KeyCode == Enum.KeyCode.Q then --This one we don't short circuit like the above two, so that we can add more inputs later. if mouse.Target and tools[mouse.Target.Name] then --You have to access the property every time you check it. Saving 'mouse.Target' does not save the mouse property to the variable, it saves the *value* of that property to the variable. print("accepted item detected, taking it now :D") target:Destroy() local tool = tools[mouse.Target.Name] tool:Clone().Parent = player.Backpack --You have to set the Parent of the Clone, not the Tool itself, or this code will only ever work for one tool of each type. end end end)
Also another thing you could do is, is a magnitude check. So basically if the mouse is over the object check how close you are so you can't pick something up from across the map
local torso = game.Players.LocalPlayer.Character:FindFirstChild("Torso") local magnitude = (Torso.Position - part2.Position).magnitude local player = game.Players.LocalPlayer local mouse = player:GetMouse() local tools = { Item1 = game.ReplicatedStorage.Item1; --No need to Clone here since we Clone them later, below. Item2 = game.ReplicatedStorage.Item2; Item3 = game.ReplicatedStorage.Item3; Item4 = game.ReplicatedStorage.Item4; } game:GetService('UserInputService').InputBegan:connect(function(input,proc) if proc then return end --This is a useful shortcut for Event-connected functions, to avoid the typical Lua indentation levels. if input.UserInputType ~= Enum.UserInputType.Keyboard then return end if input.KeyCode == Enum.KeyCode.Q then --This one we don't short circuit like the above two, so that we can add more inputs later. if magnitude < 5 then if mouse.Target and tools[mouse.Target.Name] then --You have to access the property every time you check it. Saving 'mouse.Target' does not save the mouse property to the variable, it saves the *value* of that property to the variable. print("accepted item detected, taking it now :D") target:Destroy() local tool = tools[mouse.Target.Name] tool:Clone().Parent = player.Backpack --You have to set the Parent of the Clone, not the Tool itself, or this code will only ever work for one tool of each type. end end end end)
Should work a little like that if this doesn't work just message me on roblox and I'll try to fix it :) but adding a little more to adark's post