Script sometimes spawns parts with y value ~-340282346*10^30

Basically, I have a script that chooses a random basepart or model parented to another object, and sets its CFrame to a certain position, and gives it one of four random orientations. However, sometimes the Y value gets set to an absurdly low value, and I dont know why.

The script:

local Hills = game.Workspace.Hills
local Tile = Hills.BasicTiles:GetChildren()[math.random(1, #Hills.BasicTiles:GetChildren())]:Clone()

local randomX = math.random(-1, 1)
local randomZ = math.random(-1, 1)
if randomX ~= 0 and math.random(1, 2) == 2 then
	randomZ = 0
else
	randomX = 0
end
if Tile:IsA("Model") then
	Tile:PivotTo(CFrame.new(3950, 12.5, 3950))
	Tile:PivotTo(CFrame.lookAt(Tile.PrimaryPart.Position, Tile.PrimaryPart.Position +Vector3.new(randomX, 0, randomZ)))
	Tile.Parent = game.Workspace
else
	Tile.CFrame = CFrame.new(3950, 12.5, 3950)
	Tile.CFrame = CFrame.lookAt(Tile.Position, Tile.Position + Vector3.new(randomX, 0, randomZ))
	Tile.Parent = game.Workspace
end

Is it only when a part is moved that it sets it to that low Y value, just when models are moved, or for either?

I think its either
post char limit

I believe that the source of this problem is you using two changed to CFrame or two :PivotTo calls in one frame.

Changing it to one change of the pivot fixed it:

local Hills = game.Workspace.Hills
local Tile = Hills.BasicTiles:GetChildren()[math.random(1, #Hills.BasicTiles:GetChildren())]:Clone()

local randomX = math.random(-1, 1)
local randomZ = math.random(-1, 1)
if randomX ~= 0 and math.random(1, 2) == 2 then
	randomZ = 0
else
	randomX = 0
end

if Tile:IsA("Model") then
	Tile:PivotTo(CFrame.lookAt(Vector3.new(3950, 12.5, 3950), Tile.PrimaryPart.Position + Vector3.new(randomX, 0, randomZ)))
	Tile.Parent = game.Workspace
	print(Tile.PrimaryPart.Position.Y)
else
	Tile.CFrame = CFrame.lookAt(Vector3.new(3950, 12.5, 3950), Tile.Position + Vector3.new(randomX, 0, randomZ))
	Tile.Parent = game.Workspace
	print(Tile.Position.Y)
end

that makes sense. :smiley:
ill try it

Note: had to edit second param of the pos so it looks at the right position.

sorry for the very late reply, but it is actually still happening with this code.

code:

local Hills = game.Workspace.Hills
local Tile = Hills.BasicTiles:GetChildren()[math.random(1, #Hills.BasicTiles:GetChildren())]:Clone()

local randomX = math.random(-1, 1)
local randomZ = math.random(-1, 1)
if randomX ~= 0 and math.random(1, 2) == 2 then
	randomZ = 0
else
	randomX = 0
end

if Tile:IsA("Model") then
	Tile:PivotTo(CFrame.lookAt(Vector3.new(3950, 12.5, 3950), Vector3.new(3950+randomX, 12.5, 3950+randomZ)))
	Tile.Parent = game.Workspace
	print(Tile.PrimaryPart.Position.Y)
else
	Tile.CFrame = CFrame.lookAt(Vector3.new(3950, 12.5, 3950), Vector3.new(3950+randomX, 12.5, 3950+randomZ))
	Tile.Parent = game.Workspace
	print(Tile.Position.Y)
end

image

actually, i figured it out. There was a chance it tried to look at itself, so it just bugged out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.