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

Why is it not spawning where I want it? (cloned object)

Asked by 3 years ago
local Clickd = script.Parent.ClickDetector
local Button = script.Parent
local RS = game:GetService("ReplicatedStorage")
local EndFolder = RS:WaitForChild("End")
local Pillar = EndFolder.Pillar
local Radius = 100
local X = math.random(0,Radius)
local Y = 0
local Z = math.random(0,Radius)


Clickd.MouseClick:Connect(function()
    local ClonedPillar = Pillar:Clone()
    ClonedPillar.Parent = workspace
    ClonedPillar.Position = Vector3.new(X,Y,Z)
end)

-- Pos 0, -10, 0
-- Size 512, 20, 512

--1 -119, 0.5, -62

My cloned object is not spawning where I want it to, I've tried this with many other scripts and it doesn't work, one with an instance, the "Explosion" instance spawns at the middle of the baseplate. While the current script here spawns at one position, nothing else

0
Where do you want it to spawn? LeedleLeeRocket 1257 — 3y
0
You wanted a random position for X and Z, or? R4nd0ml0l2 105 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Hi! Your code is choosing a number between 0 and 100 for once. To fix this, you simply can do a loop for the values X and Z. Heres a possible result:

local Clicked = script.Parent.ClickDetector
local Button = script.Parent
local RS = game:GetService("ReplicatedStorage")
local EndFolder = RS:WaitForChild("End")
local Pillar = EndFolder.Pillar
local Radius = 100
local X -- Nill value.
local Y = 0
local Z -- Nill value.

Clicked.MouseClick:Connect(function()
    print("work")
    local ClonedPillar = Pillar:Clone()
    ClonedPillar.Parent = workspace
    ClonedPillar.CFrame = CFrame.new(X,Y,Z)
end)

while true do -- A random value of 0 to 100 will be generated every time.
    wait() -- Making sure Roblox won't crash.
    X = math.random(0,Radius)
    Z = math.random(0,Radius)
end

It'll simply just generate a number every 0.03 seconds and if you click on the part, you can see it randomly generates to the given coordinates! If it doesn't work, then you can ask why it doesn't and what the error is. Have a good day!

0
thanks it worked! RandyN56 23 — 3y
Ad

Answer this question