Help with saving multiple variables

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

Sorry, but could you please describe your issue further regarding your code (and what you’re trying to do)? It helps quite a bit.

If you want to store multiple values within a single variable, consider using tables (if you don’t know what they are, this tutorial can help you).

2 Likes

so i want partOption1 to store multiple parts, and when needed, get the part out of the variable and use it in the script you see above

a table is probably the best way to go about it but i’m not very good at using tables

how would i get/find the specific part out of the table?

1 Like

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:

table.remove(ExampleTable, table.find(ExampleTable, "Hello!"))

And if it helps, studio also tells you about the parameters of functions while you write code.

Hope this helped!

3 Likes

Im going to show more of the script and table

–Larger Part Of Script


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

I also added the table in this script

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.