Quick summary of what my code does;
When you activate a proximity prompt, the proximity prompt finds the parent and then a string value inside of the proximity prompt with the value set to the items name, and if the items name is the same as a tool inside of a folder called “Tools” located in Server Storage; it clones it inside the players backpack.
Some things I’ve considered adding involve an animation that plays when the player collects an item.
How I want to improve the code is to make it easier to work with instead of putting a string value inside of every proximity prompt and make the code more clean.
Here’s my code
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:FindFirstChild("Modules", true)
local Tools = ServerStorage:FindFirstChild("Tools", true)
local InsertModule = require(script:FindFirstChild("InsertModule", true))
for i, v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("ProximityPrompt") then
local Handle = v.Parent
local ToolName = v:FindFirstChild("ToolName", true)
InsertModule.Spawn("GlintParticle", Handle)
if not ToolName then
warn("ToolName is not a valid member of " .. v.Name)
return
end
local Item = Tools:FindFirstChild(ToolName.Value, true)
v.ObjectText = ToolName.Value
v.Triggered:Connect(function(plr)
local Character = plr.Character or plr.CharacterAdded:Wait()
if Character:WaitForChild("Humanoid", true).Health <= 0 then
return
elseif plr.Backpack:FindFirstChild(ToolName.Value, true) or Character:FindFirstChild(ToolName.Value, true) then
return
end
if Item then
local ToolClone = Item:Clone()
ToolClone.Parent = plr.Backpack
end
end)
end
end
Any feedback will be appreciated!
The code and items are located inside of a folder in the workspace.