- What do I want to achieve?
I wanted a system where you could pick up items using a bag like in Dead Rails!
------- I also want to clarify, I added a system where it will spawn random items when you do something!
- What is the issue?
The issues are that it is unreliable sometimes it works and sometimes it doesn’t work.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried many solutions, including scrolling the Devforum but I can’t find any solution!
In advance, thank you !
My Script in SeverScriptService:
local CollectionService = game:GetService("CollectionService")
local remoteEvent = game.ReplicatedStorage.RemoteEvent
local part = game.Workspace.spawn
local folder = game.ReplicatedStorage.randomItems
local module = require(script:WaitForChild("ReturnAmount"))
local x = part.Size.X * 0.5
local y = part.Size.Y * 0.5
local z = part.Size.Z * 0.5
game.Players.PlayerAdded:Connect(function(player)
local value = Instance.new("StringValue", player)
value.Name = "recentPickedItem"
end)
function returnItem()
local randomItems = math.random(1, #folder:GetChildren())
local item = folder:GetChildren()[randomItems]
local clone = item:Clone()
return clone
end
local debounce = true
remoteEvent.OnServerEvent:Connect(function(player)
local clone = returnItem()
local prompt = clone.prompt
clone.Parent = game.Workspace
clone.Position = part.Position + Vector3.new(math.random(0 - x, x), math.random(0 - y, y), math.random(0 - z, z))
clone.Anchored = false
clone.CanCollide = false
clone.Touched:Connect(function(hit)
if hit.Name == "Baseplate" then
clone.CanCollide = true
end
end)
prompt.Triggered:Connect(function(player)
local amount = module.ReturnAmount()
if debounce == true and amount ~= 11 then
debounce = false
local recentItemValue = player:FindFirstChild("recentPickedItem")
local clonedItem = clone:Clone()
clonedItem.Parent = nil
local success, errorMessage = pcall(function()
clonedItem.Parent = player.Backpack
end)
local tags = CollectionService:GetTagged("recentTag")
if success and tags then
for _, v in ipairs(tags) do
v:RemoveTag("recentTag")
v:Destroy()
end
task.wait()
print(clonedItem.Parent)
clonedItem.Parent = player.Backpack
clonedItem:AddTag("recentTag")
recentItemValue.Value = clonedItem.Name
debounce = true
elseif success and not tags then
task.wait()
clone:Destroy()
print(clonedItem.Parent)
clonedItem.Parent = player.Backpack
clonedItem:AddTag("recentTag")
recentItemValue.Value = clonedItem.Name
debounce = true
else
warn(errorMessage)
end
else
warn("To many duplicates of items")
end
end)
end)
My module script, used for accessing the amount in the folder making sure I couldn’t pick up something when my backpack is full and for displaying my amount on the backpack:
local module = {}
function module.ReturnAmount()
game.Players.PlayerAdded:Connect(function(player)
local amount = 0
for i, v in pairs(player.Backpack:GetDescendants()) do
if v:IsA("Part") or v:IsA("TrussPart") then
amount +=1
end
end
return amount
end)
end
return module
Finally my local Script located in my tool ()
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local tool = script.Parent
local buildboardGui = tool.Handle.BillboardGui
local text = buildboardGui.Amount
local CollectionService = game:GetService("CollectionService")
local UserInputService = game:GetService("UserInputService")
task.wait(1)
local recentItemValue = player:WaitForChild("recentPickedItem")
if not recentItemValue then
local success, errorMessage = pcall(function()
recentItemValue = player:WaitForChild("recentPickedItem")
end)
if success then
return
else
warn(errorMessage)
end
end
local returnAmountModule = require(script:WaitForChild("ReturnAmount"))
local allowTOinput = nil
local debounce = true
local function userInput()
UserInputService.InputBegan:Connect(function(input, gameProcesseedEvent)
if gameProcesseedEvent then return end
if input.KeyCode == Enum.KeyCode.F and allowTOinput == true then
local amount = returnAmountModule.ReturnAmount()
if amount >= 1 then
local item = CollectionService:GetTagged("recentTag")
if not item then return end
local clone
for _, v in ipairs(item) do
if debounce == true then
debounce = false
clone = v:Clone()
clone.Parent = workspace
clone.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,0,-4)
clone:RemoveTag("recentTag")
v:Destroy()
task.wait(.3)
debounce = true
end
end
elseif amount == 0 then
warn("Cant drop, no items to drop")
return
end
end
end)
end
local function onEquipped()
allowTOinput = true
userInput()
end
local function onUnequipped()
allowTOinput = false
end
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
while true do
task.wait(.1)
local amount = returnAmountModule.ReturnAmount()
if amount ~= 11 then
text.Text = amount .. "/ 10"
elseif amount >= 11 then
return
end
end
If there are any questions, you are free to ask !