How do I make the event fire once?

Mouse.Button1Down:Connect(function()
	print("Stpe 1")
	for _ , House in ipairs((CollectionService:GetTagged("House"))) do
		local Magitude = (House:GetPrimaryPartCFrame().Position - HumanoidRootPart.Position).magnitude
		House.ClickDetector.MouseClick:Connect(function()
			Remote:FireServer(House.ClickDetector.Parent)
			end)
		end
	end)
end

The remote fires multiple times. Fires once the first time, Then twice the 2nd time and ect.

image An example

I dont know how to fix this. Any advice?

2 Likes

If I don’t got this wrong, you fire this event every time someone uses the left mouse button.

Yes? The click is to run the for loop, the event is activated by a click detector. I attempted to use :disconnect for the event but that seemed to not work. Also tried to denounce. Nothing seemed to fix the problem.

Try working like this instead:

script.parent.MouseButton1Down:Connect(function()	
    RemoteEvent:FireServer(REMOTEEVENT)
end)

I need the click detector as it opens up a ui on a building.

So what is your plan exactly? What should the script do?
Answer the basic questions that are needed in a scripting support thread.

Well pretty much I use tags to identify the item that the player placed down. And then I get the click detector to open up a ui that has the stats. But I can’t just use mouse.target because the grid system I made breaks the script.

If you just want to open a GUI when a player clicks a part use:

Localscript:

game.Workspace.PATHTOTHEPART.ClickDetector.MouseClick:Connect(function()
	script.Parent.Frame = not(script.Parent.Frame.Visible)
end) --replace script.parent.frame to your path to your GUI

But then I would need to place that script in all 50 of the models

You can also loop trough all these Parts. It would look like this:

for _,PATHTOTHEPART in ipairs(Parts:GetChildren()) do
    PATHTOTHEPART.ClickDetector.MouseClick:Connect(function()
        script.Parent.Frame = not(script.Parent.Frame.Visible)
    end) --replace script.parent.frame to your path to your GUI
end

Parts is a Folder with all PATHTOTHEPARTs inside of it.

1 Like

You don’t have to do Button1Down and MouseClick, just mouseClick


for _ , House in ipairs((CollectionService:GetTagged("House"))) do
	House.ClickDetector.MouseClick:Connect(function()
		Remote:FireServer(House.ClickDetector.Parent)
	end)
end


In that case it only runs once, there will always be more houses being placed. Can I iterate through the for loop again with another remote?

this should do the trick

this will work for when you just create the houses, but since your using CollectionService, wherever you use :AddTag in your script make sure to type updateClickConnections() right after (you will want to delete the childadded and childremoved events at the bottom)



local eventConnections = {}

local function updateClickConnections()
for _, v in pairs(eventConnections) do
v:Disconnect()
v = nil
end
for _ , v in pairs((CollectionService:GetTagged("House"))) do
	table.insert(eventConnections, v.ClickDetector.MouseClick:Connect(function()
		Remote:FireServer(v.ClickDetector.Parent)
	end))
end
end

updateClickConnections()

workspace.ChildAdded:Connect(function()
updateClickConnections()
end)
workspace.ChildRemoved:Connect(function()
updateClickConnections()
end)
3 Likes

I had no idea you can do that…

yeah theres a lot to lua lol

30 char

1 Like

Thanks, it fixed the problem. I was going crazy over this and you solved it in 5 sec.

lol np. i use this in my game too

Ohh, nice. Also the remote event opens a ui so the player can destroy the part on sever side. The problem I’m having with that is, The Menu re opens while the menu is open. I know I can denounce but what would be the proper way to do that in this case?

The player can click on the house again while menu is open. And it opens another menu overtop of the old one.

can you send the code thats on the server script your talking about?