Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Problems with clone of a model in ReplicatedStorage?

I have a button that, when clicked, clones a model from ReplicatedStorage. The clone follows the player's cursor and locks to a grid area. When a certain part of the model is clicked, it is locked into position and no longer follows the cursor.

local mouse = game.Players.LocalPlayer:GetMouse()
local testdesk = game.ReplicatedStorage.TestDesk:Clone()

game:GetService("UserInputService").InputBegan:connect(function(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.R and script.Parent.Rotate.Value == false then
		script.Parent.Rotate.Value = true
	elseif inputObject.KeyCode == Enum.KeyCode.R and script.Parent.Rotate.Value == true then
		script.Parent.Rotate.Value = false --When R is pressed, the model is rotated
	elseif inputObject.KeyCode == Enum.KeyCode.LeftShift or inputObject.KeyCode == Enum.KeyCode.RightShift then
		script.Parent.Parent.Parent.Cancel.Visible = false
		testdesk.Parent = nil
		script.Parent.Parent.Parent.Active.Value = false
	end --When Shift is pressed, the model disappears
end)

script.Parent.MouseButton1Down:connect(function()
	if script.Parent.Parent.Parent.Active.Value == false then
		script.Parent.Parent.Parent.Active.Value = true
		script.Parent.Parent.Parent.Cancel.Visible = true
		testdesk.Parent = workspace
		testdesk.PrimaryPart = testdesk.Primary
		for i,v in pairs (testdesk:GetChildren()) do
			if v.Name ~= "Primary" then
				v.Transparency = .5
				v.CanCollide = false
			end --This makes the model transparent while it follows the cursor
		end
		while script.Parent.Parent.Parent.Active.Value == true do
			if mouse.Target ~= nil then
				if mouse.Target.Name == "Even" and mouse.Target.Parent ~= testdesk and script.Parent.Rotate.Value == true then
					testdesk:SetPrimaryPartCFrame(mouse.Target.CFrame * CFrame.Angles(0, math.pi/2, 0))
				elseif mouse.Target.Name == "Even" and mouse.Target.Parent ~= testdesk and script.Parent.Rotate.Value == false then
					testdesk:SetPrimaryPartCFrame(mouse.Target.CFrame * CFrame.Angles(0, 0, 0))
				end --This maintains the model's rotation when it moves on the grid
			end
			wait()
		end
	end
end)

script.Parent.Rotate.Changed:connect(function()
	print("changed")
	testdesk:SetPrimaryPartCFrame(testdesk.Primary.CFrame * CFrame.Angles(0, math.pi/2, 0))
end) --This rotates the model

script.Parent.Parent.Parent.Cancel.MouseButton1Down:connect(function()
	script.Parent.Parent.Parent.Cancel.Visible = false
	testdesk.Parent = nil
	script.Parent.Parent.Parent.Active.Value = false
end) --This has the same effect as pressing the Shift key

testdesk.Primary.ClickDetector.MouseClick:connect(function()
	if script.Parent.Parent.Parent.Active.Value == true then
		script.Parent.Parent.Parent.Cancel.Visible = false
		script.Parent.Visible = false
		script.Parent.Parent.Parent.Active.Value = false
		for i,v in pairs (testdesk:GetChildren()) do
			if v.Name ~= "Primary" then
				v.Transparency = game.ReplicatedStorage.TestDesk:FindFirstChild(v.Name).Transparency
				v.CanCollide = true
			else v.Transparency = 1
			end
		end
	end
end)--This locks the model into position when a certain part is clicked

Here's what the GUI and the model look like:

ScreenGui
	Active (A BoolValue)
	Cancel (A TextButton)
	ScrollingFrame
		DeskButton
			Rotate (A BoolValue)
			Local Script
TestDesk
	A bunch of parts
	Primary (A Part)

The problem here is that since the clone of the model is determined before any events occur, which means that only one clone of the model can exist at any given time. Eventually I will make a script that will allow the player to place multiple desks or other models, and this clone problem is going to make that impossible. How can I fix this without ruining my script?

1 comment

0

That is simple. When the placing gets done, redo Line 2.

1 answer

So basically when the player is done placing an item, clone the item again and allow them to place it again.

This discussion is preserved as it was. New answers, comments, and votes are closed.