Teleport something to the ground when I bring it into workspace

So I’m trying to make something go to the ground when it spawns it, and I have no clue how to go about this, I looked everywhere and couldn’t find something that would work

the script brings it from lighting into workspace, I need a way to bring it down to the ground whenever it gets brought into workspace

1 Like

Update the location of the part when it’s added to workspace. For instance, create a script under serverScriptService.

function movePart(part)
    part.Position = Vector3.new(0, 0, 0)  -- Move part to (0 ,0, 0)
end)

game.workspace.ChildAdded:Connect(movePart)

Provided your ground would usually be mostly flat, you can use a raycast from the part downwards in order to figure out what the Y coordinate should be and CFrame it there.

If you expect to have more bumpy terrain, the same philosophy still applies, but you can calculate rotation tweaks to make it fit better with a raycast’s normal vector. Given your part’s shape, it’ll likely never be perfect, but it should be fairly decent.

3 Likes

This is a different attack but the same concept, it works but it floods the console… do u know why it would be doing this?

script.Parent.Parent.Changed:Connect(function()
	if script.Parent.Parent == workspace then
		local rayOrigin = script.Parent.Position
		local rayDirection = Vector3.new(0, -100, 0)

		local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
		
		script.Parent.Position = raycastResult.Position
		--script.Parent.Position.Y += (script.Parent.Size.Y/2)
		wait(10)
		script.Parent:Destroy()
	end
end)

1 Like

Yessir, a raycast does not guarantee it’ll hit something. When that happens, it’ll return nil.

This means that you should check that the raycastResult actually exists:

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
if raycastResult  then
	script.Parent.Position = raycastResult.Position
end

This will solve any errors.

Theoretically in some cases, your ray could be too short or will never have anything in its path, so you may have to increase its length or alter the ray a tad. However, if you said it works aside from the errors, you should be okay.

whenever I walk under it, it goes up to my head, I think its sending the raycast over and over again, which is weird because the parent isnt changing…

If it repeats it is most probably because you are checking when it Changes, it will repeat a raycast because its property is changing (position or whatever), you should use GetPropertyChangedSignal to check when its parent changes, check if its workspace, and raycast to find the nearest object by the Y axis.

1 Like

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