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