Ive been trying to figure this out but i cant, when ever multiple people touch the ForceBox the partOption1 variable only keeps one part so when multiple people touch it, it just won’t work for the previous person.
How can i fix this?
–Heres a snippet of the script
local partOption1
while task.wait(0.1) do
if Build1 then
for i, part in pairs(Build1:GetChildren()) do
if part:isA("BasePart") then
if partOption1 and weldOption1 then
local distance = (partOption1.Parent.PrimaryPart.Position - part.Position).Magnitude
print(distance)
if distance < 5 and partOption1.Name == part.Name then
print("COOL")
part.Transparency = 0
part.CanCollide = true
partOption1:Destroy()
partOption1 = nil
weldOption1:Destroy()
weldOption1 = nil
if #PartStoreOption1 == 0 then
MovingTruckProp:Destroy()
CurrentlyBuild = false
Constructing = false
ToolValue = 0
partOption1 = nil
weldOption1 = nil
MovingTruckProp = nil
break
else
print("still good")
end
end
end
end
end
end
end
end
btw it sets partOption1 in a different part of the script
Here’s a quick example of how you can use tables. Use this to your liking, and let me know if you have any further questions.
local ExampleTable = {} -- Just a table
table.insert(ExampleTable, "Hello!") -- appends "Hello!" to the end of the table
print(ExampleTable) -- '{ [1] = "Hello!" }'
task.wait(1) -- just for the sake of this example. imagine code in between here
local ElementPosition = table.find(ExampleTable, "Hello!") -- gets the position of "Hello!" in the table
table.remove(ExampleTable, ElementPosition) -- removes "Hello!" from the table, from it's corresponding position.
print(ExampleTable) -- '{}'
It’s quite intuitive, really. You can also combine functions and do things like these:
local partsOption1 = {}
MovingTruckProp.ForceBox.Touched:Connect(function (hit)
print("check")
if hit.Name ~= "BluePrint" then
local charHit = hit.Parent
local hum = charHit:FindFirstChild("Humanoid")
print(charHit)
if hum then
if #PartStoreOption1 ~= 0 and not charHit:FindFirstChild("Weld") then
local tableNumber = #PartStoreOption1
local randomNumFromTable = math.random(tableNumber)
local part = PartStoreOption1[randomNumFromTable]:Clone()
table.remove(PartStoreOption1, randomNumFromTable)
part.Parent = game.Workspace
part.Position = Vector3.new(0,20,0)
if part:FindFirstChild("WeldConstraint") then
for i, weldConstraint in pairs(part:GetChildren()) do
if weldConstraint:isA("WeldConstraint") then
weldConstraint:Destroy()
end
end
end
part.Transparency = 0
part.CanCollide = false
part.Parent = charHit
table.insert(partsOption1, part)
local weld = Instance.new("Weld")
weld.Parent = charHit
weld.Part0 = charHit.PrimaryPart
weld.Part1 = part
weld.C0 = CFrame.new(0,5,0)
weldOption1 = weld
end
MovingTruckProp.ForceBox.CanTouch = false
task.wait(0.5)
MovingTruckProp.ForceBox.CanTouch = true
end
end
end)
while task.wait(0.1) do
if Build1 then
for i, part in pairs(Build1:GetChildren()) do
if part:isA("BasePart") then
if part:FindFirstChild("WeldConstraint") then
if partsOption1 and weldOption1 then
local partOption1 = table.find(partsOption1, part)
print(partOption1)
if partOption1 then
local distance = (partOption1.Parent.PrimaryPart.Position - part.Position).Magnitude
print(distance)
if distance < 5 and partOption1.Name == part.Name then
print("COOL")
part.Transparency = 0
part.CanCollide = true
partOption1:Destroy()
partOption1 = nil
weldOption1:Destroy()
weldOption1 = nil
if #PartStoreOption1 == 0 then
MovingTruckProp:Destroy()
CurrentlyBuild = false
Constructing = false
ToolValue = 0
partOption1 = nil
weldOption1 = nil
MovingTruckProp = nil
break
else
print("still good")
end
end
end
end
end
end
end
end
end
end
i noticed it wasnt working so i added print(partsOption1) and it prints out nil