The script looks fine to me, if someone could please try to fix my issue as it will not spawn on click. If someone could also add an additional script to remove the object on click too, thanks.
local Object = game.ReplicatedStorage.Guy --Guy is my object in replicatedstorage
local ObjectSpawn = game.Workspace.ObjectSpawn --Object Spawner
function onClicked()
local CloneObject = Object:Clone()
CloneObject.Parent = game.Workspace
CloneObject:MoveTo(ObjectSpawn.Position)
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
I would say to check if the guy is in workspace or not, likely not the most efficient way to do this.
local Object = game.ReplicatedStorage.Guy local ObjectSpawn = workspace.ObjectSpawn function onClicked() local Guy = workspace:FindFirstChild("Guy") if Guy then Guy:Destroy() else local CloneObject = Object:Clone() CloneObject.Parent = workspace CloneObject:MoveTo(ObjectSpawn.Position) end end script.Parent.ClickDetector.MouseClick:Connect(onClicked)
So what i did was mimic your setup i placed a model named
Guy
in the ReplicatedStorage with a part inside of the model.
Next in the
Workspace
I placed a part named
ObjectSpawner
with a ClickDetector in it. Then i placed the following script in the ObjectSpawner Part.
``` script.Parent.ClickDetector.MouseClick:Connect(function()
if not game.Workspace:FindFirstChild("Guy") then
local ObjectClone = game.ReplicatedStorage:FindFirstChild("Guy"):Clone()
ObjectClone.Parent = game.Workspace
ObjectClone:MoveTo(game.Workspace.ObejectSpawner.Position + Vector3.new(0,2,0))
else
game.Workspace:FindFirstChild("Guy"):Destroy()
local ObjectClone = game.ReplicatedStorage:FindFirstChild("Guy"):Clone()
ObjectClone.Parent = game.Workspace
ObjectClone:MoveTo(game.Workspace.ObejectSpawner.Position + Vector3.new(0,2,0))
end
end) ```
It worked completely fine for me.