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)
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.