Spawning a model help

The cloned balls spawn about 8-10 studs higher than they should fsr.

need to spawn at line 1 not 2 where they’re spawning
image

In the output these 2 lines should be the same, what am I missing?

--//SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//VARIABLES
local SpawnsFolder = ReplicatedStorage:WaitForChild("Spawns")
local spawnedItems = game.Workspace:WaitForChild("SpawnedItems")
local gameModel = game.Workspace:WaitForChild("GameModel")
local ramps = gameModel:WaitForChild("Ramps"):GetChildren()

local button = script.Parent

--//FUNCTIONS
local function getLocation()

	local RNG = math.random(1,2)
	
	return ramps[RNG]
end

--//EVENTS
button.MouseButton1Click:Connect(function()
	local clone = SpawnsFolder:FindFirstChild("Ball"):Clone()
	
	local spawnPart = getLocation()
	
	print("The spawnParts Y vector position is:",spawnPart.CFrame.Position.Y)
	
	local coordX = math.random(-spawnPart.Size.X/2, spawnPart.Size.X/2)
	local coordY = spawnPart.CFrame.Position.Y + 3
	local coordZ = spawnPart.CFrame.Position
	
	print("The position of coordY is:",coordY)
	
	clone:PivotTo(spawnPart.CFrame + Vector3.new(coordX, coordY, coordZ))
	
	print("Y coord of ball is:", clone.Ball.CFrame.Position.Y)
	clone.Parent = spawnedItems
end)

1 Like

I’m guessing you want to spawn a ball 3 studs above the spawnPart, but your end result goes like this

1. get random X value between the width of the spawnPart
2. get the world position of the spawnPart, Y and Z values specifically
3. clone a ball.
4. move the ball to the world position of the spawnPart + [[ the world position of the spawnPart, Y and Z specifically ]]

step 4 is equivalent to:
clone:PivotTo(
	spawnPart.CFrame
	+ Vector3.new(
		math.random(-spawnPart.Size.X / 2, spawnPart.Size.X / 2)
		spawnPart.CFrame.Position.Y + 3, -- two times the height of spawnPart + 3 studs
		spawnPart.CFrame.Position -- the entire Vector3?
	)
)

CFrames in every Part are always relative to the world (position [0, 0, 0], rotation [0, 0, 0]). Adding a Vector3 to a CFrame with move its position by the amount of the Vector3. In your case, you added the Y position of the spawnPart’s CFrame to itself plus 3 studs. You also used its position (Vector3) for the Z axis of the new position as well.

Remove the spawnPart.CFrame.Position lines in coordY. I don’t know what you will use coordZ for though, but if it’s for setting the ball’s Z position, remove that and replace it with zero.

Roblox has links to CFrames. You can check them out if you want:

1 Like

Yeah I forgot about 0 when setting it all up. Since the ramps had a gap between them where the ball shouldn’t spawn I chose to randomly choose a ramp to spawn on and returned that as the spawnPart. Then I figured I would get the CFrame of the spawnPart so I could move the model anywhere. Then I just needed the height and I thought I needed Z axis also. It works now and spawns 3 studs up to spawn clear of the ramp so it can roll down the slope.

Tyvm for the help.

2 Likes

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