Randomized rotation doesn't work

I’m trying to having the rotation of cactuses randomized everytime the map spawned in:

local mapstorage = game.Workspace.MapStorage
local cactus = script.Parent.Cactus:GetChildren()

game.Workspace.MapStorage.ChildAdded:Connect(function()
	if mapstorage:FindFirstChild("Bone Desert") then
		cactus.Rotation = Vector3.new(0, Random, 0)
	else
		return end
end)

What am I doing wrong?

GetChildren() returns a table. Also for 3D objects it’s Orientation not Rotation

1 Like

You’ll also need to use math.random(minimum, maximum) instead of random I believe.

1 Like

Yep that’s true but, Random.new() also works. It’s based on the same algorythm as math.random iirc

2 Likes
local mapstorage = game.Workspace.MapStorage
local cactus = {
	script.Parent.Cactus.a,
	script.Parent.Cactus.b,
	script.Parent.Cactus.c,
	script.Parent.Cactus.d,
	script.Parent.Cactus.e,
	script.Parent.Cactus.f,
	script.Parent.Cactus.g,
	script.Parent.Cactus.h,
	script.Parent.Cactus.i,
	script.Parent.Cactus.j,
	script.Parent.Cactus.k,
	script.Parent.Cactus.l,
	script.Parent.Cactus.m,
	script.Parent.Cactus.n,
	script.Parent.Cactus.o,
	script.Parent.Cactus.p,
	script.Parent.Cactus.q,
	script.Parent.Cactus.r
}

game.Workspace.MapStorage.ChildAdded:Connect(function()
	if mapstorage:FindFirstChild("Bone Desert") then
		local x = math.random(0,360)
		for i, Part in pairs(cactus) do
			Part.Orientation = Vector3.new(0, x, 0);
		end
	end
end)

This is my new script

Oml. Please just use an for i,v loop on GetChildren ; - ;

1 Like

I think this should work? The parents and stuff might be wrong though

for _, Cactus in pairs(mapstorage["Bone Desert"].Cactus:GetChildren()) do
Cactus.CFrame *= CFrame.new(0, math.pi * math.random() * 2, 0)
end

This is assuming the cactuses are parts. If they’re models, make sure they have a primary part and use Cactus:SetPrimaryPartCFrame(Cactus.PrimaryPart.CFrame * CFrame.new(0, math.pi * math.random() * 2, 0))

1 Like

Just keep local cactus = script.Parent.Cactus:GetChildren()

1 Like
local mapstorage = game.Workspace.MapStorage
local cactus = script.Parent.Cactus:GetChildren()

game.Workspace.MapStorage.ChildAdded:Connect(function()
	if mapstorage:FindFirstChild("Bone Desert") then
		local x = math.random(0,360)
		cactus.Orientation = Vector3.new(0, x, 0);
	end
end)

I don’t get it, this is my script rn

Looks alright. Did you try it out?

1 Like

it doesnt work so yeah and yes but yeah and yes

I am confusion? so yeah and yes but yeah and yes what.

1 Like

yeah i did it for the amount of characters, but i think i see what’s wrong. A orientation of 360 doesnt work at all

Ah alright. Explains why. But can you show me your explorer/workspace?

1 Like

I think i got it now, i changed it to this

local x = math.random(-180,180)

Try it out so yeah and yes but yeah and yes

1 Like

nope still doesnt work yes sad and yes when

I tried this in studio and it worked. Script was in ServerScriptService

local Map = workspace.MapStorage

Map.ChildAdded:Connect(function(vMap)
	if vMap.Name == "Bone Desert" then
		for _, Cactus in pairs(vMap.Cactuses:GetChildren()) do
			Cactus.CFrame *= CFrame.Angles(0, math.pi * math.random() * 2, 0)
		end
	end
end)
1 Like
local mapstorage = game.Workspace.MapStorage
local cactus = script.Parent.Cactus:GetChildren()

game.Workspace.MapStorage.ChildAdded:Connect(function()
	if mapstorage:FindFirstChild("Bone Desert") then
		for i,v in pairs(cactus) do
			local x = math.random(-180, 180)
			v.Orientation = Vector3.new(0, x, 0);
		end
	end
end)
1 Like

both doesnt work lol lmao lol yeah lmao lol