Code issues - table and color change in frame

Hey guys, what’s wrong in my code? i’m trying to make a list where if you touches a part frame’s color changes, idk why change color is not working :frowning:

image

image

LocalScript:

-- items
local it1 = script.Parent.item1
local it2 = script.Parent.item2
local it3 = script.Parent.item3

-- Touches parts
local tpart1 = game.Workspace.Touching1 -- RED
local tpart2 = game.Workspace.Touching2 -- BLUE
local tpart3 = game.Workspace.Touching3 -- GREEN

-- tables
local obj = {}

-- touches
tpart1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(obj,1,it1)
		print("touches red part")
		wait(5)
		it1.BackgroundColor3 = BrickColor.new("Bright red")
		wait(5)
		print(obj)
	end
end)

tpart2.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(obj,1,it2)
		print("touches blue part")
		wait(5)
		it2.BackgroundColor3 = BrickColor.new("Bright blue")
		wait(5)
		print(obj)
	end
end)

tpart3.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(obj,1,it3)
		print("touches green part")
		wait(5)
		it3.BackgroundColor3 = BrickColor.new("Bright green")
		wait(5)
		print(obj)
	end
end)


-- end touches
local remove = game.Workspace.remove

tpart1.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.remove(obj,1)
		print("item 1 has been removed")
		wait(2)
		table.remove(obj,1)
		print("item 2 has been removed")
		wait(2)
		table.remove(obj,1)
		print("item 3 has been removed")
		wait(2)
		print("All items has been removed!")
	end
end)

Replace this:

it1.BackgroundColor3 = tpart1.Color
it2.BackgroundColor3 = tpart2.Color
it3.BackgroundColor3 = tpart3.Color
1 Like

Thanks you! do you know what’s wrong here? i change this code:

image

table.remove(obj,1)

You remove the 1st object everytime, change the number 1 to 2 and the other number 1 to 3.

1 Like

Great! thanks you!! and how can i make it happen only once, if i have touched it once?

To locate the workspace it is not necessary to place:

local remove = game.Workspace.remove

Just use:

local remove = workspace.remove
1 Like

Guys got an error here :frowning:

this white part is called remove

I think it’s probably mixing. :Remove / :remove from your code, change the name to something else;

1 Like

Thansk you guys a lot! :smiley: :smiley:

That worked? Just checking lol

1 Like

Yes, it worked perfectly! :smiley: thanks you

You’re welcome, happy to help ^ - ^
As a bonus, this is better:

local debonce = true
workspace[Name].Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and debonce then
    	debonce = false
	    for i=1, #obj do
		    print("item", i, "has been removed")
	    	wait(2)
    	end
		obj = {}
	    print("All items has been removed!")
    	debonce = true
	end
end)
1 Like

Oh thanks you alot!! obj = {} does it remove all items? :scream:

what does it mean? → [name] :scream:

The name of what to touch.

Yes.