What's wrong with this simple script?

Yes, another simple script that I have no idea what I’m doing wrong on.

Could someone help me debug? No errors, the script is for a block that unanchors when touched, and goes back to the original position after 5 seconds.

Code:

local db = false

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and not db then
		db = true
		local hum = hit.Parent:FindFirstChild("Humanoid")
		hum.Sit = true
		local posx = -99.188
		local posy = 206.42
		local posz = 1425.02
		script.Parent.Anchored = false
		script.Parent.CanCollide = false
		wait(4)
		script.Parent.Anchored = true
		script.Parent.Position = Vector3.new(posx,posy,posx)
		script.Parent.CanCollide = true
		wait(1)
		db = false
	end
end)

Thanks!

Is it running at all? Also are you using a script or a local script.

Well the first thing is to check if it’s even getting into the if statement, add a print inside there. If the print is not printing, one of the conditions is not truthful

And as @RiccoMiller said, what type of script is it?

Normal script, it does run, just doesn’t return the block to the position.

Make sure that the block doesn’t fall in the void or gets destroyed

Maybe instead of this

script.Parent.Position = Vector3.new(posx,posy,posx)

Try

script.Parent.CFrame = CFrame.new(posx,posy,posx)

Or it could be that since it’s CanCollide off and unanchored, the brick may’ve disappeared into the void by the time it’s time to put it back

The blocks disappear after a couple seconds… why is that?

It’s because it’s UnCollideable and unanchored, it fell into the void and got destroyed

I edited the code so that the CanCollide is on, but the blocks still disappear.

I did a few calculations, and figured out that the block would fall 1569.6 studs in 4 seconds if it’s in freefall and if the workspace gravity is 196.2 studs/seconds^2. Once the blocks fall in the void, they get destroyed. You can configure the FallenPartsDestroyHeight property of the workspace if you want to.

I think my error was

  1. The CanCollide issue
  2. I was setting the Z pos to the X

It’s working now, thank you all.

1 Like

Just a quick question, if I were to set something’s orientation, what would I use? (e.g. Vector3, CFrame)

You can use the Orientation property which is a vector3, or you could multiply its CFrame with another one.

1 Like