-
What do you want to achieve?
I want to be able to make a for _,i in pairs() do thing in workspace constantly be checking/looping. The script is in ServerScriptService and is meant to handle boxes that give you items when touched. -
What is the issue?
I have zero idea on how to make this, I have searched. What I have tried has resulted in duplicates of items. -
What solutions have you tried so far?
RunService, while loops, repeat until, ChildAdded, ChildRemoved, etc. None worked for me.
Here is my current script:
local players = game:GetService("Players")
local Serverstorage = game:GetService("ServerStorage")
local items = Serverstorage.Tools.Guns:GetChildren()
for _,item in pairs(workspace:GetDescendants()) do
if item.Name == "ItemBox" and item:IsA("BasePart") then
item.Touched:Connect(function(hit)
local db = false
if hit.Parent.ClassName == "Model" and hit.Parent:FindFirstChild("Humanoid") and players:FindFirstChild(hit.Parent.Name) then
if db ~= false then return end
db = true
local backpack = players[hit.Parent.Name]:FindFirstChild("Backpack")
local item2 = items[math.random(1,#items)]:Clone()
item2.Parent = backpack
print("please")
item:Destroy()
end
end)
end
end