Cloning a model, then putting it to a location is causing the model to appear several times

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    To clone a model to a location without issues.
  2. What is the issue? Include screenshots / videos if possible!
    Title/This actually causes lag and it puts strain on my weak low-end 2gb system…
    Here is a video about it:

Here’s the script that clones the model/put

local Players = game:GetService("Players")
local function CloneToWorkspace(object)
	local player
	repeat
		player = Players:GetPlayerFromCharacter(object)
		object = object.Parent
	until player or not object
	
	if not player or player ~= Players.LocalPlayer then
		return
	end

	local waitForChild = game.WaitForChild
	local modelclone = waitForChild(game:GetService("ReplicatedStorage"), "TrussModel")
	local model = modelclone:Clone()
	local Truss = Instance.new("TrussPart")
	local Maze = script.Parent.Parent:WaitForChild("The Maze (Eazy)")
	Maze.Button.Light.BrickColor = BrickColor.new(0,0,0)
	Truss.Anchored = true
	Truss.CanCollide = true
	Truss.BrickColor = BrickColor.new("Dark stone grey")
	Truss.Name = "Truss1"
	Truss.Position = Vector3.new(-71.56, 13.625, 1302.028)
	Truss.Size = Vector3.new(2, 24, 2)
	Truss.Parent = Maze
	model.Name = "Trusses"
	wait(3)
	Maze.TransparentPiece.CanCollide = true
	wait(7)
	model.Parent = Maze
end

local connection = script.Parent.Parent:WaitForChild("The Maze (Eazy)").Button.Light.Touched:Connect(CloneToWorkspace)
1 Like

It is creating a new truss model every time the event is called. Therefore, you can prevent this by disconnecting the event.

local connection
local Players = game:GetService("Players")
local function CloneToWorkspace(object)
	local player
	repeat
		player = Players:GetPlayerFromCharacter(object)
		object = object.Parent
	until player or not object
	
	if not player or player ~= Players.LocalPlayer then
		return
	end

	local waitForChild = game.WaitForChild
	local modelclone = waitForChild(game:GetService("ReplicatedStorage"), "TrussModel")
	local model = modelclone:Clone()
	local Truss = Instance.new("TrussPart")
	local Maze = script.Parent.Parent:WaitForChild("The Maze (Eazy)")
	Maze.Button.Light.BrickColor = BrickColor.new(0,0,0)
	Truss.Anchored = true
	Truss.CanCollide = true
	Truss.BrickColor = BrickColor.new("Dark stone grey")
	Truss.Name = "Truss1"
	Truss.Position = Vector3.new(-71.56, 13.625, 1302.028)
	Truss.Size = Vector3.new(2, 24, 2)
	Truss.Parent = Maze
	model.Name = "Trusses"
	connection:Disconnect()
	wait(3)
	Maze.TransparentPiece.CanCollide = true
	wait(7)
	model.Parent = Maze
end

connection = script.Parent.Parent:WaitForChild("The Maze (Eazy)").Button.Light.Touched:Connect(CloneToWorkspace)
1 Like

Check how many times the function is being called.Ussually .Touched is called more the once when touched by a player

1 Like

@Meat_Make Wait a minute, so how can I make it so that when you press the button once you cannot press it again and trigger the event?

1 Like

@Galactiq But if I do that, would the rest of the callback code not execute or will it continue?

To recap, the part where it creates the truss from scratch doesn’t cause any problem at all. The problem is with the other one (the model (called TrussModel) that contains over 100 trusses)… Though, that is kinda weird because it didn’t cause any issues (it only created 1 model) in the directory in my old script. I had to change it because I only wanted the player themselves to see it. After that, then it was causing problems. I couldn’t build all of the trusses in that model because there was a lot of those and I didn’t want to spend time just to build a specific part (the script was already over ~25 lines)… Note that accordingly to the script, the truss model gets cloned from game.ReplicatedStorage and then get’s cloned to the model (Called The Maze (Easy) that is in game.Workspace).

Nevermind, I fixed that part by simply disabling the script when pressed. I realised that it would run the rest of the callback code, even if disabled, which is a good thing on my case.

If you’re referring to the code that is after the event is disconnected, then yes, it would still run as usual. Disconnecting an existing connection simply means it will not run when the event is fired. So after it is touched once, it will no longer fire when touched again.

Well, i could do the same thing by disabling the script. As you said:

Disabling the script altogether does these two same things!!!