Mousebutton1down event doesnt run

Basically, my local script event doesnt work and it doesnt run the remote event because of that
Here’s the video

External Media

and the scripts

local script:

local rs = game:GetService("ReplicatedStorage")
local startergui = game:GetService("StarterGui")
local colorbuttons = startergui.ScreenGui.colors


local buttons = {colorbuttons.Red, colorbuttons.Green, colorbuttons.Blue}
local colors = {"Really red", "Lime green", "Really blue"}

for i, v in pairs(buttons) do
	v.MouseButton1Down:Connect(function()
		rs.colorevent:FireServer(colors[i])
	end)
end

rs.colorevent.OnClientEvent:Connect(function(plr, message)
	startergui.ScreenGui.colors.Visible = false
end)

and the server script.

local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")

rs.colorevent.OnServerEvent:Connect(function(plr, color)
	print(plr.Name.." selected "..color.." color")
	local brick = ss:FindFirstChild(plr.Name.."_2")
	brick.BrickColor = BrickColor.new(color)
	rs.colorevent:FireClient(plr)
end)

That’s all, also I did check and remotevent is in ReplicatedStorage, and the player’s rock is in ServerStorage

Thanks.

You tried changing .MouseButton1Down to .Activated?

1 Like

Shouldn’t you fire v.MouseButton1Down outside the for i, v loop?

the for loop makes the .MouseButton1Down event work on every element in the buttons list

try doing this and move it to the bottom of the script. I think I had trouble with this in the past and this fixed it for me. But you will need to add a cooldown, so it doesn’t fire the remote event a million times (I don’t think this is a very good script but it might fix the problem)

while true do
	for i, v in pairs(buttons) do
		v.MouseButton1Down:Connect(function()
			rs.colorevent:FireServer(colors[i])
		end)
	end
        task.wait()
end

Yea, that looks like it would cause more problems. Because you’re continually creating new connections each time. You really just need to do Connect once for each button.

1 Like

Yeah, I don’t know any other way lol. If anyone finds out it would be helpful.

Guys thanks for the ideas, im just kinda busy and I cant do that rn, I will do that in like 10-20 mins i guess.

does it shows any error? i fix a lot of my codes when an error throws, btw you should print() on all functions, just for knowing when does the code stops working

1 Like

Was just about to say.
Because the code itself looks fine. But maybe there was some “external setup” that was missed.

It doesnt show any error, I tried using print statements and they stopped working exactly on the event handler

I was gonna suggest doing a print between these lines

for i, v in pairs(buttons) do
	v.MouseButton1Down:Connect(function()

with print(i,v)

I think i tried that, but it didnt work, maybe i didnt try that, idek.

so it doesnt fires to the server?

I did that and it worked, but when i put print statement inside the event handler, it doesnt print anymore

Yes, in the server script the print statements dont work absolutely

huh… So if I’m correct, it sounds like the “buttons” table is empty?

or actually… no…

I dont think so because i created table in the local script.

So you said, when you put the print just within the loop, it did print 3 messages?

Because I think even if a table is “defined”. If the elements are nil, then it’s not “counted”.
In other words, the length could be zero.

I tried putting in print statemets like

print(i)
print(v)

and they printed what I needed