I am trying to make my part clone a Model called SimpleStructure from ReplicatedStorage to Workspace. I am getting error an error that says "SimpleStructure is not a valid member of Workspace". I Don't know how to change my script to work (More info on bottom)
SP = script.Parent -- Variable SP.ClickDetector.MouseClick:connect(function(clicked) -- When clicked, this function is called SimpleStructure = false -- Makes SimpleStructure False to clone it if game.Workspace.SimpleStructure == true then -- If SimpleStructure is true, it was cloned so destroy this part SP:Destroy() -- Destroys the part end -- This if statement is over wait(0.1) if game.Workspace.SimpleStructure == false then -- Check to see if SimpleStructure is false, meaning not cloned game.ReplicatedStorage.SimpleStructure:Clone() -- Goes to ReplicatedStorage and takes SimpleStructure wait(0.1) game.ReplicatedStorage.SimpleStructure:Clone().Parent = game.Workspace -- Clones SimpleStructure into Workspace end -- This if statement is over end)-- Function is over
I think I have a bunch of problems? I have two if statements because I want this script to not be able to be clicked multiple times and clone the Model over and over while being clicked. So I thought of making it mark SimpleStructure. I want it to be able to see if SimpleStructure is inside of Workspace or not. If it is, Destroy the brick you need to click. If it isn't Clone it into Workspace.
I cleared the script up, I can't read code with that many comments!(haha!)
I also removed a bunch of useless lines. You can't say if game.Workspace.SimpleStructure == true then
, that model doesn't hold a boolvalue! So what I did was use the FindFirstChild to check if it was in the workspace, if it wasn't we cloned it and parented it to the workspace. If it did, we removed the click part. You shouldn't use two if statements when they're actually one, use else or elseif. We used the else
statement which basically means if not
in english since we don't need to make any other checks.
Your final script should look like this.
SP = script.Parent SP.ClickDetector.MouseClick:connect(function(clicked) if game.Workspace:FindFirstChild("SimpleStructure") then SP:Destroy() else game.ReplicatedStorage.SimpleStructure:Clone().Parent = game.Workspace end end)
If you are only going to destroy()
the part if it's in the workspace, why not just use this sequence of code? If you are going to add other stuff into the if statement, use the code above. If not, you should use this code:
SP = script.Parent SP.ClickDetector.MouseClick:connect(function(clicked) game.ReplicatedStorage.SimpleStructure:Clone().Parent = game.Workspace SP:Destroy() end) --If this is all you need to do, use this!