Spawning Parts with a ClickDetector

How would I make a Ball Spawn in a certain position if a ClickDetector is clicked?
I also want it to only spawn if a certain Value is above 0?

2 Likes

Something like this in a server script:

game.Workspace.ClickDetector.MouseClick:connect(function()

if game.Workspace.MyValue.Value > 0 then

local ball = game.ReplicatedStorage.Ball:Clone()
ball.Position = Vector3.new(0,0,0)
ball.Parent = game.Workspace

end

end)

1 Like

First you’d want to have a part with a clickdetector inside it

Like this

next you’d want to

workspace.Part.ClickDetector.MouseClick:connect(function() -- this connects the detector being clicked event to the function below
    if(Value>0)then -- this checks if your value is above 0, you have to replace "Value" with the path to the value
        local sphere = Instance.new("Part") -- creates a part
        sphere.Shape = Enum.PartType.Ball -- sets the part shape to a ball
        sphere.Position = Vector3.new(x,y,z) -- replace x,y,z with where you'd like the ball to spawn
        sphere.Parent = workspace -- this sets the Parent of the part to the workspace
    end --  ends the if statement
end) -- ends the function, the ) finishes the ( in the first line
1 Like