Mobile Crosshair Error

So i’m trying to make a mobile crosshair that detects when a player pulls out a certain weapon.
I’m getting the error "Expected ‘)’ to close ‘(’ at line 13), got ‘end’
I’m probably just an idiot but I don’t see what I have to do.

local CROSSHAIR = "rbxassetid://81623697"
local MobileCrosshair = player.PlayerGui.MobileCrosshair
local knife = script.Parent.Parent.Backpack:FindFirstChild("Knife")
local revolver = script.Parent.Parent.Backpack:FindFirstChild("Revolver")
--mobile detection
local inputService = game:GetService("UserInputService")
if inputService.TouchEnabled or inputService.KeyboardEnabled 
	then if InputService.TouchEnabled then
if knife.Equipped:Connect(function(mouse)
			MobileCrosshair.Visible = true
			if knife == nil then MobileCrosshair.Visible = false
			else if revolver.Equipped:Connect(function(mouse)
						MobileCrosshair.Visible = true
						if revolver == nil then MobileCrosshair.Visible = false
						end
						end
				end)

It means that you’re either missing ends, or you’ve got too many ends

1 Like

No matter how many ends I change it still doesn’t work

1 Like

So first of all, the formatting of this is really bad, and consider organizing your code better. Here is the completed script:

local CROSSHAIR = "rbxassetid://81623697"
local MobileCrosshair = game.StarterGui.MobileCrosshair
local knife = script.Parent.Parent.Backpack:FindFirstChild("Knife")
local revolver = script.Parent.Parent.Backpack:FindFirstChild("Revolver")
--mobile detection
local inputService = game:GetService("UserInputService")
if inputService.TouchEnabled or inputService.KeyboardEnabled 
then if InputService.TouchEnabled then
		if knife.Equipped:Connect(function(mouse)
				player.PlayerGui.MobileCrosshair.Visible = true
				if knife == nil then player.PlayerGui.MobileCrosshair.Visible = false
				else if revolver.Equipped:Connect(function(mouse)
							player.PlayerGui.MobileCrosshair.Visible = true
							if revolver == nil then player.PlayerGui.MobileCrosshair.Visible = false
							end
						end)
					then
						
					end
				end
			end)
		then
			-- do stuff
		end
	end
end

Basically you did not have enough ends, that is because your code is not organized. :slight_smile:

2 Likes

thanks, I am big stupid head big stupid big stupid

1 Like

There is an if statement without a then at the end on line 13

1 Like

Be sure to mark it as the solution if this helped, it is at the bottom right at the post with a checkmark and Solution!

Now it says expected ‘end’ (to close ‘then’ at line 8), got

local MobileCrosshair = game.StarterGui.MobileCrosshair
local knife = script.Parent.Parent.Backpack:FindFirstChild("Knife")
local revolver = script.Parent.Parent.Backpack:FindFirstChild("Revolver")
--mobile detection
local inputService = game:GetService("UserInputService")
if inputService.TouchEnabled or inputService.KeyboardEnabled 
then if InputService.TouchEnabled then
		if knife.Equipped:Connect(function(mouse)
				player.PlayerGui.MobileCrosshair.Visible = true
				if knife == nil then player.PlayerGui.MobileCrosshair.Visible = false
				else if revolver.Equipped:Connect(function(mouse)
							player.PlayerGui.MobileCrosshair.Visible = true
							if revolver == nil then player.PlayerGui.MobileCrosshair.Visible = false
							end
						end)
					then
					end
				end
			end)
		then
		end

Try closing each dropdown one by one. That usually helps me when I have this issue. You can collapse a function by pressing the down arrow on the left side of the script window. Make sure that each function is ended by the corresponding end that you want it to close. Hope this helps!

uhhhhhhh what? my brain is very tiny

Collapse each function, if statement, for statement, ect. Give me a bit and I’ll see if I can fix it but I’m a bit busy rn.

These arrows can be found next to the line number that a statement or function is on.

When clicked, it will hide all of the code in the function, and shows where the function is ended.
image

Notice, in the first picture, there is only one arrow. That is most likely your problem.

my dumb brain doesn’t understand, I tried fixing it. I need someone to show me the fix

local MobileCrosshair = game.StarterGui.MobileCrosshair
local knife = script.Parent.Parent.Backpack:FindFirstChild("Knife")
local revolver = script.Parent.Parent.Backpack:FindFirstChild("Revolver")
--mobile detection
local inputService = game:GetService("UserInputService")
if inputService.TouchEnabled or inputService.KeyboardEnabled then
	if inputService.TouchEnabled then
		knife.Equipped:Connect(function(mouse)
			player.PlayerGui.MobileCrosshair.Visible = true
			if knife == nil then
				player.PlayerGui.MobileCrosshair.Visible = false 
			end
			revolver.Equipped:Connect(function(mouse)
				player.PlayerGui.MobileCrosshair.Visible = true
				if revolver == nil then
					player.PlayerGui.MobileCrosshair.Visible = false
				end
			end)
		end)
	end
end

Here’s the code. I want to remind you to keep your code organized. I found many errors other than just the end error, like the misuse of then. There was also some capitalization errors (LUA is a Case Sensitive coding language). Finally, I’m not sure if you did it already, but the player variable is not defined in the code. What that means is that you have to create a variable that is equal to the player.

I would like you to read these, too, as they will help your understanding of some of the errors you made.