Issue with code

Does anybody know why this is occurring? My developer and I cannot figure it out for the life of us.

We are not getting any output on the click but when we were, the transparency just looped over and over.

https://gyazo.com/0354a6ec503720acccc80d38ac5353b3

local MMUOn = true
local EMUOn = true
local player = game.Players.LocalPlayer
local character = player.Character

script.Parent.EMU.MouseButton1Click:Connect(function()
	print("click")
	if EMUOn == false then
		EMUOn = true
	else
		if EMUOn == true then
			EMUOn = false
		end
	end
end)

script.Parent.MMU.MouseButton1Click:Connect(function()
	print("click")
	if MMUOn == false then
		MMUOn = true
	else
		if MMUOn == true then
			MMUOn = false
		end
	end
end)

if MMUOn == false then
	for i, v in pairs(character.MMUGroup:GetDescendants()) do
		if v:IsA("BasePart") then
			v.CanCollide = false
			v.Transparency = 0
		else
			if v:IsA("Decal") then
				v.Transparency = 0.85
			end
		end

	end
	MMUOn = true
else
	if MMUOn == true then
		for i, v in pairs(character.MMUGroup:GetDescendants()) do
			if v:IsA("BasePart") then
				v.CanCollide = true
				v.Transparency = 1
			else
				if v:IsA("Decal") then
					v.Transparency = 1
				end
			end
		end
		MMUOn = false
	end
end

if EMUOn == false then
	for i, v in pairs(character.EMUGroup:GetDescendants()) do
		if v:IsA("BasePart") then
			v.CanCollide = false
			v.Transparency = 0
		else
			if v:IsA("Decal") then
				v.Transparency = 0.85
			end
		end
	end
	EMUOn = true
else
	if EMUOn == true then
		for i, v in pairs(character.EMUGroup:GetDescendants()) do
			if v:IsA("BasePart") then
				v.CanCollide = true
				v.Transparency = 1
			else
				if v:IsA("Decal") then
					v.Transparency = 1
				end
			end
		end
		EMUOn = false
	end
end
2 Likes

Just a little thing I noticed wanted to point out
you this

if EMUOn == false then
		EMUOn = true
	else
		if EMUOn == true then
			EMUOn = false
		end
	end

I believe this version to be more efficient instead of a double if statement you should try using elseif

if EMUOn == true then
	EMUOn = false
elseif EMUOn == false then
	EMUOn =  true
end

or you can try just

if EMUOn == true then
	EMUOn = false
else -- if its false or set to any other value 
	EMUOn =  true
end

Where does the script is like game.Workspace or player.PlayerGui?

This won’t change much performance-wise but I’ll possibly adapt the code for that.

This is a LocalScript stored inside of a ScreenGui

I am aware i just believe it makes the code more easy to read

To make it less you can just make this:

EMUOn = not EMUOn

1 Like

This entire part of the code should only run once
Is the script being cloned/are there multiple?
Is there more to the script like a loop surrounding the whole thing?
Have you tried putting print statements inside the if EMUOn / MMUOn statements to see if theyre the things running over and over again causing the problem?

It seems very weird that something that runs once would infinitely loop over and over again, given the information Im given

You can change the ScreenGui.ResetOnSpawn to false.

local MMUOn = true
local EMUOn = true
local player = game.Players.LocalPlayer
local character = player.Character

script.Parent.EMU.MouseButton1Click:Connect(function()
	print("click")
	if EMUOn == false then
		EMUOn = true
	else
		if EMUOn == true then
			EMUOn = false
		end
	end
end)

script.Parent.MMU.MouseButton1Click:Connect(function()
	print("click")
	if MMUOn == false then
		MMUOn = true
	else
		if MMUOn == true then
			MMUOn = false
		end
	end
end)

if MMUOn == false then
	for i, v in pairs(character.MMUGroup:GetDescendants()) do
		if v:IsA("BasePart") then
			v.CanCollide = false
			v.Transparency = 0
		else
			if v:IsA("Decal") then
				v.Transparency = 0.85
			end
		end

	end
	MMUOn = true
else
	if MMUOn == true then
		for i, v in pairs(character.MMUGroup:GetDescendants()) do
			if v:IsA("BasePart") then
				v.CanCollide = true
				v.Transparency = 1
			else
				if v:IsA("Decal") then
					v.Transparency = 1
				end
			end
		end
		MMUOn = false
	end
end

if EMUOn == false then
	for i, v in pairs(character.EMUGroup:GetDescendants()) do
		if v:IsA("BasePart") then
			v.CanCollide = false
			v.Transparency = 0
		else
			if v:IsA("Decal") then
				v.Transparency = 0.85
			end
		end
	end
	EMUOn = true
else
	if EMUOn == true then
		for i, v in pairs(character.EMUGroup:GetDescendants()) do
			if v:IsA("BasePart") then
				v.CanCollide = true
				v.Transparency = 1
			else
				if v:IsA("Decal") then
					v.Transparency = 1
				end
			end
		end
		EMUOn = false
	end
end

That is the entirety of the script, I’m presuming it’s something to do with the for loop and that it’s looping the transparency of the objects over and over again.

What good will that do us?

The if statements and for in pairs loops at the very bottom of your script is not connected to any events and also isn’t within a function is that intentional. @TrojanHorseAttack

The if statements don’t need to be connected to any events. Last time I checked, MMUOn and EMUOn were both booleans.

Both buttons do not respond to clicks, they don’t print anything in the output:
https://i.gyazo.com/57f4553c2e858c40650895e064262830.mp4

I mean for the purpose of the original purpose it would just be easier to wrap them into a function and call them when needed.

You are correct but what is there to gain I’m only trying to help him improve the quality of his code entirely.

I chose to not wrap them into a function and instead change the boolean values of the variables when called.

What is the parent of the script?
script.Parent.EMU.

The gui is cloned when we put on a spacesuit, so don’t worry about it being inside ServerStorage

image

Im thinking that might be the issue
Where is the code where you clone it?

I was going to recommend you disable the local script before parenting back into the player**.
But I’m not sure that would change anything.
Since local scripts only work within decedents of a player.
I will test to see if it needing to be disabled could be the problem brb.

Putting it into a different place seemed to sort the issue.