Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

expected identifier when parsing statement got 'then'?

i have no idea whats wrong with my code


local doorvar = workspace.door.door1
local doorvar2 = workspace.door.door2
local ClickDetector = workspace.door.door1.ClickDetector
if doorvar.CanCollide == true and ClickDetector.MouseClick:Connect(function() then
	local sound = Instance.new("Sound")
	sound.SoundId = "http://www.roblox.com/asset/?id=833871080"
	sound.Volume = 0.3
	sound:play()
	doorvar.Transparency = 1
	doorvar.CanCollide = false
	doorvar2.Transparency = 0
	doorvar2.CanCollide = true
	end
end)

if doorvar2.CanCollide == true and ClickDetector.MouseClick:Connect(function() then
		local sound2 = Instance.new("Sound")
		sound2.SoundId = "http://www.roblox.com/asset/?id=833871080"
		sound2.Volume = 0.3
		sound:play()
		doorvar.Transparency = 0
		doorvar.CanCollide = true
		doorvar2.Transparency = 1
	doorvar2.CanCollide = false
	end
end)



####Please encode your code in a code block, the community will not help you with your question if you don't encode your code as it's hard to read!

Thanks, JesseSong!

2 answers

you are doing this:

if doorvar.CanCollide == true and ClickDetector.MouseClick:Connect(function() then

Which is wrong because you are checking if door can collide, okay, nice but then you are adding and there for no reason, in this case it would check if the second given argument is true but your second argument is connection to a function. Instead try this:

ClickDetector.MouseClick:Connect(function() -- Whenever button is pressed, it will run the function
	if doorvar.CanCollide then -- If the door is collideable then do something
		-- your code
	end
end)

Basically whenever the ClickDetector is clicked, it will run the function and then it will check if the door is collideable and if it is then it will do something, else it won't do anything. You should do the same with lines 16+

Answer:

Step-By-Step Explanation:

The error code:

Expected identifier when parsing statement got 'then' means

The code was expecting an expression when parsing/closing an input to the code and hence resulted in an error. ##Problem:

  • You cannot reference an event when referring to an if statement in the same line, or an error will result!

For example:

You CANNOT do this:

if doorvar.CanCollide == true and ClickDetector.MouseClick:Connect(function() then

##Solution:

  • Also make sure that you check your if statements on your code.

##Recommended:

  • Make sure you use a code block when you're typing/pasting a script.

##Fixed script:

local doorvar = workspace.door.door1
local doorvar2 = workspace.door.door2
local ClickDetector = workspace.door.door1.ClickDetector
ClickDetector.MouseClick:Connect(function(onMouseClick)
if doorvar.CanCollide == true and ClickDetector then
	local sound = Instance.new("Sound", game.Workspace)
		sound.SoundId = "rbxassetid://639750143" -- its better to reference the rbxassetid
	sound.Volume = 0.3
	sound:play()
	doorvar.Transparency = 1
	doorvar.CanCollide = false
	doorvar2.Transparency = 0
	doorvar2.CanCollide = true
end
end)

if doorvar2.CanCollide == true  and ClickDetector then
	local sound2 = Instance.new("Sound")
	sound2.SoundId = "rbxassetid://639750143"
	sound2.Volume = 0.3
	sound2:play()
	doorvar.Transparency = 0
	doorvar.CanCollide = true
	doorvar2.Transparency = 1
	doorvar2.CanCollide = false
end

print "yes"


If you need any more assistance, please feel free to leave a comment! If you want to support me, mark this as an accepted answer!

Sorry for bad grammar as this was heavily rushed.

####PS: This took about 20 mins, to set up the answer edit it, and test it.

##References:

Scripting Resources

(These are the most common useful resources to help you learn Lua, and of course, you can use YouTube but make sure you use the developer hub for reference!)

ROBLOX Developer hub

Scripting Helpers

Roblox Developer/DevForum

Thanks, JesseSong! (Re-edited, August 18, 2021)

Fixed Grammatical Errors!

This discussion is preserved as it was. New answers, comments, and votes are closed.