Script doing opposite of intended purpose?

So I have this script that should move transparent and uncollide everything besides itself. isntead it does the opposite, any help?

script.Parent.MouseClick:Connect(function()
	if script.Opened.Value == false then
				local p = script.Parent.Parent.Parent:GetChildren()
			for i=1, #p do
				if not (p[i].Name == "Dog") then
		p.Transparency = 1
		p.CanCollide = false
						script.Parent.Parent.Parent.Dog.Texture.Transparency = 1
						script.Parent.Parent.Parent.Dog.Texture2.Transparency = 1
						script.Parent.Parent.Parent.Dog.Transparency = 1	
			local d = script.Parent.Parent.Parent.Parent.OpenedLGate:GetChildren()
			for i=1, #d do
				if not (d[i].Name == "Dog") then
	d.Transparency = 0
	d.CanCollide = true
			    script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Texture.Transparency = 0
						    script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Texture2.Transparency = 0
			  script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Transparency = 1
	script.Parent.Parent.Parent.Parent.OpenedLGate.Clicker.ClickDetector.Script.Opened.Value = false
	script.Opened.Value = true
end
	end
	end
end
	end
	end)

image

1 Like

So I’m sorry, but I’d like to start off by commenting about your formatting. It’d be far easier to read and try and help figure out where an issue arises, if the code was formatted in a readable manner. That being said, formatting your code makes it look like:

script.Parent.MouseClick:Connect(function()
	if script.Opened.Value == false then
		local p = script.Parent.Parent.Parent:GetChildren()
		for i=1, #p do
			if not (p[i].Name == "Dog") then
				p.Transparency = 1
				p.CanCollide = false
				script.Parent.Parent.Parent.Dog.Texture.Transparency = 1
				script.Parent.Parent.Parent.Dog.Texture2.Transparency = 1
				script.Parent.Parent.Parent.Dog.Transparency = 1	
				local d = script.Parent.Parent.Parent.Parent.OpenedLGate:GetChildren()
				for i=1, #d do
					if not (d[i].Name == "Dog") then
						d.Transparency = 0
						d.CanCollide = true
						script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Texture.Transparency = 0
						script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Texture2.Transparency = 0
						script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Transparency = 1
						script.Parent.Parent.Parent.Parent.OpenedLGate.Clicker.ClickDetector.Script.Opened.Value = false
						script.Opened.Value = true
					end
				end
			end
		end
	end
end)

I think certain aspects would be more comprehensive, if you made variables for things that often get re-used; so for instance.

local ClosedLGate = script.Parent.Parent.Parent;
local OpenedLGate = script.Parent.Parent.Parent.Parent.OpenedLGate;
-- make sure this is the right hierarchy as it's not provided in the image so I can't verify.

and then work the details out in your head or on a piece of paper. For instance in the formatted version I have you’d notice you’re looping through and setting parts multiple times, which imo isn’t needed. So we’re down to:

local ClosedLGate = script.Parent.Parent.Parent;
local OpenedLGate = script.Parent.Parent.Parent.Parent.OpenedLGate;

local function ToggleTextures(Object, ExpectedValue)
    Object.Texture.Transparency = ExpectedValue;
    Object.Texture2.Transparency = ExpectedValue;
    Object.Transparency = ExpectedValue;
end

script.Parent.MouseClick:Connect(function()
    if not script.Opened.Value then
        script.Opened.Value = true
        for i,v in pairs(ClosedLGate:GetChildren()) do
            if v:IsA("BasePart") and v.Name ~= "Dog" then
                v.Transparency = 1; v.CanCollide = false;
            end
        end
        for i,v in pairs(OpenedLGate:GetChildren()) do
            if v:IsA("BasePart") and v.Name ~= "Dog" then
                v.Transparency = 0; v.CanCollide = true;
            end
        end
        ToggleTextures(ClosedLGate.Dog, 1); ToggleTextures(OpenedLGate.Dog, 0)
    end
end

and one last thing I’d like to note it’d be easier to set up a toggle, when you’re only messing with one toggle value.

1 Like

Hello.
Assuming your script is completely error-free, I think the problem is after line 10.

In fact, your script actually does what you want it to do. However, you are ordering almost opposite commands after line 10 without any conditional statement or delay periods. As a result, the objects remain on CanCollide true and visible.

This might be a solution:

script.Parent.MouseClick:Connect(function()
	if script.Opened.Value == false then
				local p = script.Parent.Parent.Parent:GetChildren()
			for i=1, #p do
				if not (p[i].Name == "Dog") then
		p.Transparency = 1
		p.CanCollide = false
						script.Parent.Parent.Parent.Dog.Texture.Transparency = 1
						script.Parent.Parent.Parent.Dog.Texture2.Transparency = 1
						script.Parent.Parent.Parent.Dog.Transparency = 1	
			script.Opened.Value = true
else
local d = script.Parent.Parent.Parent.Parent.OpenedLGate:GetChildren()
			for i=1, #d do
				if not (d[i].Name == "Dog") then
	d.Transparency = 0
	d.CanCollide = true
			    script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Texture.Transparency = 0
						    script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Texture2.Transparency = 0
			  script.Parent.Parent.Parent.Parent.OpenedLGate.Dog.Transparency = 1
	script.Parent.Parent.Parent.Parent.OpenedLGate.Clicker.ClickDetector.Script.Opened.Value = false
	script.Opened.Value = false
end
	end
	end
end
	end
	end)

Pay attention to the changes made on lines 11, 12 and 22. Feel free to fix the indentation. Also, note NonessentialCoder’s suggestions. They will help you improve your scripting.

I hope this helped a bit…