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

Help with Conditional Statements?[Not Fixed]

So,I was making a script that only executes one time if it's Touched but everything was find until I added TouchedEnded event,why and how do I fix it?

I also got an error saying:

Workspace.Part.Script:12: unexpected symbol near 'then'

Script:

local Part = script.Parent
local Touched = false--Debounce
--------------------------
	Part.Touched:connect(function(TouchedPart)
		Touched = true
		FoundHumanoid= TouchedPart.Parent:FindFirstChild("Humanoid")--Checks if it's a humanoid
				if not FoundHumanoid then
					print("You're not a Character "..TouchedPart.Name.." !")
					Touched = false
				else
					if not Touched or Part.TouchedEnded:connect(function()
						then
						print("I haven't been touched!")
							Touched = false
						Part.Transparency = 1
					else
						
						Part.Transparency = 0.5
							print("I've been touched")
						
				end	
			end
		end)
	end)

1 answer

✓ Accepted answer

Well, you cannot use an event in a conditional statement. Instead you should use the :wait() method of events to check whether the event has occurred or not. This is how it should look like.

local Part = script.Parent
local Touched = false--Debounce

    Part.Touched:connect(function(TouchedPart)
        Touched = true
        FoundHumanoid= TouchedPart.Parent:FindFirstChild("Humanoid")--Checks if it's a humanoid
                if not FoundHumanoid then
                    print("You're not a Character "..TouchedPart.Name.." !")
                    Touched = false
                else
                    if not Touched or Part.TouchEnded:wait()  then
                        print("I haven't been touched!")
                            Touched = false
                        Part.Transparency = 1
                    else
                        Part.Transparency = 0.5
                         print("I've been touched")
                end 
            end
    end)

2 comments

0

I got a new error saying: TouchedEnded is not a valid member of Part.

0

My mistake, it is "TouchEnded"

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