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

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

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

2 comments

0

Where do you want it to spawn?

0

You wanted a random position for X and Z, or?

1 answer

✓ Accepted answer

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!

1 comment

0

thanks it worked!

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