Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Object Spawns on Click?

Asked by 5 years ago

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)

0
I made a copy of your script and it worked fine The_Pr0fessor 595 — 5y
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) The_Pr0fessor 595 — 5y
0
I only answered here because the answer box didn't work The_Pr0fessor 595 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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)
0
Where did he say he wanted to see if the guy was already there? The_Pr0fessor 595 — 5y
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" samdoggo9 0 — 5y
0
ohhhhh im geekin u right The_Pr0fessor 595 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

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. OfficialBrendon 0 — 5y
0
U must have put the script in a click detector instead of the actual part The_Pr0fessor 595 — 5y

Answer this question