Part wont spawn back

so i made this simple script where if the part falls at a specific magnitude it spawns back on the baseplate, however it’s not working. I try distance < spawnbackdist and it worked in the way i didnt what it to. Am i doing smth wrong?


local part = script.Parent
local baseplate = game.workspace.Baseplate
	
local distance = (part.Position - baseplate.Position).Magnitude

local spawnbackdist = 360

if  distance >= spawnbackdist then 
		
	print(distance)
	print("spawned")
	part.Position = baseplate.Position + Vector3.new(0,160,0)
end

You’re only checking the part’s position once! Once the if statement is checked, it isn’t checked again. You would need to use a RunService.PostSimulation connection to check the part’s position after the simulation updates.

local RunService = game:GetService("RunService")

local part = script.Parent
local baseplate = game.workspace.Baseplate

local spawnbackdist = 360

RunService.PostSimulation:Connect(function()
	local distance = (part.Position - baseplate.Position).Magnitude

	if distance >= spawnbackdist then 
		print(distance)
		print("spawned")
		part.Position = baseplate.Position + Vector3.new(0,160,0)
	end
end)
6 Likes

I see, so i guess I did need run service after all. I also didn’t know i had to use Poststimulation nor do i know what that does or is. Thanks! :head_shaking_vertically::+1:

RunService.PostSimulation fires after physics simulation has stepped, 240 times a second. Alternatively there’s PreSimulation, which is the same but fires before the physics simulation has stepped. This lets you do “last minute” changes before physics is updated.

2 Likes

that’s interesting, thanks i know now

1 Like