Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't my script detecting a position change in a part? [Solved]

I'm trying to make a light part "go out" when its position changes, but for some reason my script isn't detecting a change in the part's position. It does work if I manually change the position in the properties or use the studio move tool, but when I throw some explosive at it or use :BreakJoints(), the part moves but the script doesn't detect it.

Here is the script :

for i,v in pairs(script.Parent:GetDescendants()) do -- In a folder full of parts
	if v:IsA('BasePart') then
		v:GetPropertyChangedSignal('Position'):Connect(function()
			v.Material = Enum.Material.SmoothPlastic
			v.BrickColor = BrickColor.Black()
			v.PointLight.Enabled = false
		end)
	end
end

I tried other ways like using the .Changed event or checking the orientation but it still doesn't work. This seems like a really simple thing to do, but apparently it's giving me problems.

Also, the part is welded, not anchored, which is why I'm trying to detect its change in position.

10 comments

0

Position doesn't fire Changed event.

0

^ :GetPropertyChangedSignal() too? If so, how else can I do this?

0

Doesn't fire GetPropertyChangedSignal either

0

You could have an infinite loop if there is no event that works for it. I am sure that there would be an event but I don't know what it would be.

0

You could have a variable which has the value of the old position and if the current position is different then do code

0

^ I did do that, but it um...it didn't work, at least not correctly.

0

I've tested it and Position does fire on changed event

0

^ Well, it didn't fire for me. Did you have to manually move it or did you use an explosion to move it?

0

I manually moved it but ill test it

0

I think it's because un-anchor doesn't change property.

2 answers

✓ Accepted answer

This should work but may cause lag if moved a lot

local Folder = script.Parent
local Timer = Folder.Timer

while true do 
	wait(5)
	for i,v in pairs(Folder:GetChildren()) do
		if v:isA("BasePart") then 
			v.Touched:connect(function() -- I used this function to test it on server so I didn't need to move it manually
				v.Position = v.Position + Vector3.new(0,0,1)
			end)
			v:GetPropertyChangedSignal("Position"):Connect(function()
				print("Move Items") -- put your code here
			end)
		end
	end
end

The problem with your code was not that it was wrong but the start execution. It needs to constantly running to work, so while true do should fix this (or you could make a timer on a intValue and do Timer.Changed:Connect(function) instead of using while true do.)

If this doesn't work for you tell me.

1 comment

0

I had to make some changes to your script since some parts messed up the lights, but I see what you were trying to do and it works, well at least in certain situations, but I can fix that. I guess I'll accept your answer. :P

Never mind, I solved it myself. I had to use a while loop and clone a script to all the light parts. Here is the script if you're interested in seeing it :

while wait() do
	if script.Parent:IsA('BasePart') then
		if script.Parent.Velocity.X >= .0001 then
			script.Parent.BrickColor = BrickColor.Black()
			script.Parent.Material = Enum.Material.SmoothPlastic
			script.Parent.PointLight.Enabled = false
			script.Parent.Name = 'BrokenLight'
			script:Destroy()
			break
		end
	end
end

Although it works, I feel like it's not the best way since I have to use a while loop constantly and clone the script to lots of parts. If you think you know a better way to do this, just answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.