Hey, i am not an expert when it comes to programming, and with that i need help sometimes, so i want help with a simple script, not actually a full script just a part of it.
The objective of the script is: Clone a model into the workspace from time to time at random defined positions, for example, i have 4 different positions and from time to time that model will be cloned and put in that position.
I just need help with one thing: I don’t know how to randomize the position. I don’t actually mean randomize it, the objective is having for example 5 positions i choose and get one of those to put the model in that position.
I’m open for questions, this is probably easy and i’m just asking for simple stuff lol. Any help would be good, thanks!
It’s pretty simple, I didn’t make the parts move, but this should help you.
game.Players.PlayerAdded:Connect(function()
local Result = math.random(1,4)
local FinalNumber = 0
local part = game.ReplicatedStorage.part ---Assuming you are using replicated storage.
local partclone = part:Clone()
partclone.Parent = Workspace
FinalNumber = Result
if FinalNumber == 1 then
end
if FinalNumber == 2 then
end
if FinalNumber == 3 then
end
if FinalNumber == 4 then
end
end)
local positions = {
[1] = 10,10,10;
[2] = 20,10,20;
[3] = 30,10,30
}
local waittime = 10
local model = --model here
while wait(waittime) do
local clone = model:Clone()
clone.Parent = workspace
local pos = (math.random(1,#positions))
clone.Position = Vector3.new(pos)
end
Have four parts inside of a model called Positions
place those parts at whatever positions you want
inside of the model you want to clone’s properties, change it’s PrimaryPart to any part inside of the model.
then, use this script:
local partsModel = workspace.Positions:GetChildren()
local model = LOCATION OF YOUR MODEL TO CLONE
while true do
--get a random part in your parts model
local randomPart = partsModel[math.random(1,#partsModel)]
local clone = model:Clone() --clone the model
clone:SetPrimaryPartCFrame(randomPart.CFrame) --move the model to the random position
clone.Parent = workspace --put the part into the workspace
wait(6) -- wait 6 seconds before cloning another one of the models into the workspace
end
While loops shouldn’t be used as “while interval do”; instead use them in scenarios as conditions “while a > b do”. At least that’s the standard practice.
I recommend utilising runservice when it comes to endlessly looping something. Secondly, I am not sure why you put the value math.random… in a pair of brackets. Looks good otherwise.
Well first of all, it wouldn’t work math.random(1,#positions) won’t generate the result you’re looking for, currently it would produce a number inbetween 1 and the size of the table. Meaning 1-4; not a vector3 value. Secondly, teaching good practice is important.
local positions = {
[1] = 10,10,10;
[2] = 20,10,20;
[3] = 30,10,30
}
local waittime = 10
local model = --model here
game:GetService("RunService").Heartbeat:Connect(function() -- changed
wait(waittime)
local clone = model:Clone()
clone.Parent = workspace
local pos = positions[math.random(1, #positions)] -- changed
clone:SetPrimaryPartCFrame(CFrame.new(Vector3.new(pos))) -- changed
end)