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

Object Spawns on Click?

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)

3 comments

0

I made a copy of your script and it worked fine

0

script.Parent.ClickDetector.MouseClick:Connect(function() local ObjectClone = game.ReplicatedStorage:FindFirstChild("Model"):Clone() ObjectClone.Parent = game.Workspace ObjectClone:MoveTo(game.Workspace.ObejectSpawner.Position + Vector3.new(0,2,0)) end)

0

I only answered here because the answer box didn't work

2 answers

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)

3 comments

0

Where did he say he wanted to see if the guy was already there?

0

he said he wanted to delete the part on click too? if thats not what he meant then he has to be more specific than "remove the object on click"

0

ohhhhh im geekin u right

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.

2 comments

0

When I put the script in the click detector, I get an error in the output for "ClickDetector is not a valid member of ClickDetector" I followed all your instructions.

0

U must have put the script in a click detector instead of the actual part

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