#Im Confused Why this is not working

6 messages · Page 1 of 1 (latest)

frosty nebula
#

and how do i make it do action like change color when touched

latent valley
# frosty nebula and how do i make it do action like change color when touched

So you seem to have an issue with your second end. as you never close the Connect() method, I would also recommend formatting your code again as the indents are wrong, this will cause confusion at what end does what

--replace ... with existing code

..
TouchPart.Touched:Connect(function(OtherPart)
  if not PartTouched then  --does the same as if PartTouched == false then in this case. Main difference is that it doesn't differentiate between false and nil, yet PartTouched will always be either true or false, so not an issue in this case.
    PartTouched = true
    print(Otherpart.Name) --the Name property is always spelled with a capital N
    
    TouchedPart.Color = Color3.New(1,0,0) --change the color to red  
    
    task.wait(2)
    PartTouched = false
    TouchedPart.Color = Color3.New(0.054902, 1, 0.00392157) --change it (back) to green
  end
end)

I would also recommend using guarded statements for your conditions, it's just a way to make your code easier to read.

--So instead of

if Condition then
  ...
end

--you type
if not Condition then return end --or continue(skip to next it.)/break (end loop) end in case of a loop.
...

--this is mostly visible with pieces of code that have a lot of conditions

if Con1 then
  ...
  if Con2 then
  
  end
end

--now imagine this with 8 conditions, yes you could string them together with booleanary logic, yet generally speaking you will encounter code where that simply isn't possible
frosty nebula
latent valley
# frosty nebula i never really understood indents and its a very big problem for me do you have ...

indents are referring to the indentations in scripts. With certain parts of the syntax (ifelse statements, loops, functions, etc.) they will create indents. These indents are not a big issue if you don't have a lot of them (generally speaking <4-5 deep). Yet past that, it can get harder to read the code. It will be hard to see which end refers to what line. And just reading the code will require an ultrawide monitor.

For now I would just keep in your head that you wish to keep them under 4-5 with the code formatted correctly. There's a good chance it won't take long until you come across a code that mimics the joke program I attached to this message.

frosty nebula
#

alright so will it usuallyy auto indent?

latent valley
# frosty nebula alright so will it usuallyy auto indent?

yes, else you can always go to the "script" menu at the top of your screen (shows up if you've a script open in the viewport)Then you can click "format selection" -> "format document" and it will automatically correct the indents for you (so the code is indented in the right way, makes it easier to read)