How can i add this to my code?

Apologies for vague title, couldn’t think of a way to describe it in a title. I am currently developing a building system, and it is going great! Issue is, once the player starts to be able to click to place, it will go on forever and my walnut brain can’t figure out a way to stop this once it has been clicked once and make it go back

Local Script in the GUI

local ScreenGui = script.Parent
local function ConnectDescendant(Descendant)
	if Descendant:IsA("TextButton") or Descendant:IsA("ImageButton") then
		Descendant.MouseButton1Down:Connect(function()
			local name = Descendant.Name
			local mouse = game:GetService("Players").LocalPlayer:GetMouse()
			mouse.Button1Up:Connect(function()
				local hit = mouse.Hit
				game:GetService("ReplicatedStorage").PlaceEvent:FireServer(name, hit)
			end)
		end)
	end
end

ScreenGui.DescendantAdded:Connect(ConnectDescendant)

for _, Descendant in ScreenGui:GetDescendants() do
	ConnectDescendant(Descendant)
end

Script in ServerScriptService

local RemoteEvent = game:GetService('ReplicatedStorage').PlaceEvent


local function ServerEvent(player, name, hit)
	local item = game:GetService("ServerStorage"):FindFirstChild(name)
	local clone = item:Clone()
	clone.Parent = game.Workspace
	clone:SetPrimaryPartCFrame(hit)
end

RemoteEvent.OnServerEvent:Connect(ServerEvent) 
5 Likes

Instead of :Connect(function() you can use :Once(function()

You could also do local Connection = mouse.Button1Up:Connect(function()
then when want to stop, you can put Connection:Disconnect() (This works for both connections, just used that one as an example)

3 Likes

May I get an example? I don’t know how to implement it.

2 Likes

Thank you, replacing the second :Connect() with Once() in the Local Script worked for me!

2 Likes

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