How do i make this script repeatedly clone when a button is clicked

local Button = script.Parent
local MainFrame = script.Parent.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("CookieEvent")
local Cookie = ReplicatedStorage:WaitForChild("Cookie")
local Proximity = game.Workspace:WaitForChild("Plate").Proximity

Button.MouseButton1Click:Connect(function()
	MainFrame.Visible = false
	Event.OnClientEvent:Connect(function()
		local Clone = Cookie:Clone()
		Clone.Parent = game.Workspace
		Clone.Anchored = false
		Clone.Position = Proximity.Position
	end)
end)

So when the button is clicked repeatedly it only clones once. How do i make it so it keeps on cloning every-time the button is pressed?

1 Like

Hey, are you trying to clone the cookie only for the client’s side or do you want this server side?

If you want it only client sided then you don’t need to do the .OnClientEvent:, just do this

local Button = script.Parent
local MainFrame = script.Parent.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cookie = ReplicatedStorage:WaitForChild("Cookie")
local Proximity = game.Workspace:WaitForChild("Plate").Proximity

Button.MouseButton1Click:Connect(function()
	MainFrame.Visible = false
	local Clone = Cookie:Clone()
	Clone.Parent = game.Workspace
	Clone.Anchored = false
	Clone.Position = Proximity.Position
end)

But if you are trying to do it on the server side then do this instead:

-- Local Script
local Button = script.Parent
local MainFrame = script.Parent.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("CookieEvent")

Button.MouseButton1Click:Connect(function()
	MainFrame.Visible = false
    Event:FireServer()
end)
-- Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("CookieEvent")
local Cookie = ReplicatedStorage:WaitForChild("Cookie")
local Proximity = game.Workspace:WaitForChild("Plate").Proximity

Event.OnServerEvent:Connect(function()
	local Clone = Cookie:Clone()
	Clone.Parent = game.Workspace
	Clone.Anchored = false
	Clone.Position = Proximity.Position
end)

Let me know if this helps!

1 Like

something like this

local Button = script.Parent
local Cookie = ReplicatedStorage:WaitForChild("Cookie")
local loopId = 0
local looping = false

local function StartLoop()
	looping = true
	loopId += 1
	local id = loopId
	while id == loopId do
		print("Loop")
		local Clone = Cookie:Clone()
		Clone.Parent = game.Workspace
		Clone.Anchored = false
		Clone.Position = Proximity.Position
		task.wait(1)
	end
end
local function StopLoop()
	looping = false
	loopId += 1
end

Button.MouseButton1Click:Connect(function()
	if looping = false then
		StartLoop()
	else
		StopLoop()
	end
end)
1 Like