Functions wont print a variable when a part with clickdetector is clicked

Hey, I am making pass code lock. Here is the script, but it doesn’t seem to be working or it just doesn’t want to print the thing I am clicking on.

local code = "123"
local input = ""

function red()
	input = input..1
	print(input)
end

script.Parent.Red.ClickDetector.MouseClick:Connect(red)

function green()
	input = input..2
	print(input)
end

script.Parent.Green.ClickDetector.MouseClick:Connect(green)

function blue()
	input = input..3
	print(input)
end

script.Parent.Blue.ClickDetector.MouseClick:Connect(blue)

The code is in a local script:
image

4 Likes

Local scripts will not run in workspace, re parent it to somewhere such as StarterPlayerScripts or replace it with a script and it should work

Nope, still doesn’t work. I remade the script a bit and put it in StarterPlayerScripts.

local code = "123"
local input = ""

function red()
	input = input..1
	print(input)
end

game.Workspace.PassLock.Red.ClickDetector.MouseClick:Connect(red)

function green()
	input = input..2
	print(input)
end

game.Workspace.PassLock.Green.ClickDetector.MouseClick:Connect(green)

function blue()
	input = input..3
	print(input)
end

game.Workspace.PassLock.Blue.ClickDetector.MouseClick:Connect(blue)

Use Instance:WaitForChild() to get the detectors, client scripts can run before all instances load.

To allow client code to run in the workspace, you can use a Script with RunContext set to client.

1 Like

Doesnt work, gives an error:
image

Script:

local code = "123"
local input = ""
local ClickDetector1 = game.Workspace.PassLock.Red:WaitForChild("ClickDetector")
local ClickDetector2 = game.Workspace.PassLock.Green:WaitForChild("ClickDetector")
local ClickDetector3 = game.Workspace.PassLock.Blue:WaitForChild("ClickDetector")

function red()
	input = input..1
	print(input)
end

ClickDetector1.MouseClick:Connect(red)

function green()
	input = input..2
	print(input)
end

ClickDetector2.MouseClick:Connect(green)

function blue()
	input = input..3
	print(input)
end

ClickDetector3.MouseClick:Connect(blue)

Could be it running before the parts are replicated, maybe use WaitForChild on the parts?

PassLock:WaitForChild(“Red”).ClickDetector
1 Like

It worked! But, what do you mean by “before the parts are replicated”?

Final Script:

local code = "123"
local input = ""

local Red = game.Workspace.PassLock:WaitForChild("Red")
local Green = game.Workspace.PassLock:WaitForChild("Green")
local Blue = game.Workspace.PassLock:WaitForChild("Blue")

local ClickDetector1 = Red:WaitForChild("ClickDetector")
local ClickDetector2 = Green:WaitForChild("ClickDetector")
local ClickDetector3 = Blue:WaitForChild("ClickDetector")

function red()
	input = input..1
	print(input)
end

ClickDetector1.MouseClick:Connect(red)

function green()
	input = input..2
	print(input)
end

ClickDetector2.MouseClick:Connect(green)

function blue()
	input = input..3
	print(input)
end

ClickDetector3.MouseClick:Connect(blue)
1 Like

Instances have to replicate and load into the client from the server, the code is running before this happened - meaning it can not find them as they do not exist at that given time

2 Likes

So essiantially:

Instances are replicated = Instances are loaded?

To replicate the instances is like to load them in?

1 Like

They are replicated and loaded onto the client, yes. For example, things in “ReplicatedFirst” are the first to load onto the client.

2 Likes

Is there an order that container services are loaded in? For example: first one to load is ReplicatedFirst, then ServerScriptService, then workspace and so on…

1 Like

Instances in ServerScriptService and ServerStorage do not replicate - they are only accessible by the server. If there is any order, I do not know.

1 Like

Thanks for helping me out! This was a hard one XD

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.