I have a brick that is supposed to (on click) disappear and lose collision for 20 seconds, this is the code, and position.
workspace > R123 > ClickDetector > Script
The script is as shown
function OnClick() if script.Parent.Parent.Transparency == 0 and script.Parent.Parent.CanCollide == true then OnClick = script.Parent.Parent.Transparency == .6 and script.Parent.Parent.CanCollide == false wait(20) script.Parent.Parent.Transparency = 0 and script.Parent.Parent.CanCollide == true end
end
script.Parent.MouseClick:Connect(OnClick)
nothing happens and the log shows
Enum.KeyCode.Unknown (x1)
even though I defined the key.
When I click on the brick, the console is claiming it is an unknown value as shown here --Enum.KeyCode.Unknown (x1)
[UPDATE]
the console randomly displays this in my mass of clicking
21:58:44.018 - Attempt to connect failed: Passed value is not a function 21:58:44.019 - Stack Begin 21:58:44.020 - Script 'Workspace.R123.ClickDetector.Script', Line 10 21:58:44.020 - Stack End
[UPDATE]
with InfernoExeuctioner's "conclusion" I get the error --
Enum.KeyCode.Unknown 22:00:57.444 - attempt to call a boolean value
[UPDATE]
Script has been edited to this, but I see no reason for it to not work other than what someone said about the "and" conditional.\
local part = script.Parent.Parent
function OnClick() if part.Transparency == 0 and part.CanCollide == true then part.Transparency = .6 part.CanCollide = false wait(20) part.Transparency = 0 part.CanCollide = true end end script.Parent.MouseClick:Connect(OnClick)
ClickDetectors do not have an event called MouseButton1Click
, however they do have one called MouseClick
, which fires when someone left clicks the part containing the ClickDetector.
I am not sure were your output is relating to a KeyCode, elaborate on that and I will edit my answer.
:connect()
is deprecated, use :Connect()
local part = script.Parent.Parent ---you should make your part variable different from the function variable --please keep in mind that you only use double equal signs when using conditional statements like if ..code.. then. function OnClick() if part.Transparency == 0 and part.CanCollide == true then part.Transparency = .6 part.CanCollide = false wait(20) part.Transparency = 0 part.CanCollide = true end end
script.Parent.MouseButton1Click:Connect(OnClick)
Will not work in this situation. MouseButton1Click
should be used for GUIs.
script.Parent.MouseClick:Connect(OnClick)
MouseClick
should be used for ClickDetectors
You should listen to the other answers, but one big error I see is on line 3 and 7.
In line 3, you tried to set a variable named OnClick
, but your function was already named OnClick
, thus confusing the script.
In line 3, you also tried to assign a property using ==
, when those are used exclusively in conditional statements.
Example:
if 2 + 2 == 4 then while 2 + 2 == 4 do repeat until 2 + 2 ==4
The repeat loop would end instantly and the while loop wouldn’t break lol.
In line 7, you tried to set the property’s value, but added an and
with no context. You use and
in conditional statements as well.
Just use this code:
local debounce = false local part = game.Workspace.MyPart -- Change this to your actual part. local clicker = part.ClickDetector -- Change this to your actual click detector. function change() if not debounce then debounce = true -- You have to wait 20 seconds before the script lets you activate the function again. part.Transparency = 0.6 part.CanCollide = false wait(20) part.Transparency = 0 part.CanCollide = true debounce = false -- You can now click on the part and activate the function. end end clicker.MouseClick:Connect(change)
Let me know if this solves your problem!
EDIT
There is a keycode error apparently, however when I try this in my own game it works perfectly fine. No keycode error...