Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited 1 year ago

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.

0
Position doesn't fire Changed event. User#19524 175 — 5y
0
^ :GetPropertyChangedSignal() too? If so, how else can I do this? User#20279 0 — 5y
0
Doesn't fire GetPropertyChangedSignal either User#19524 175 — 5y
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. User#21908 42 — 5y
View all comments (6 more)
0
You could have a variable which has the value of the old position and if the current position is different then do code User#21908 42 — 5y
0
^ I did do that, but it um...it didn't work, at least not correctly. User#20279 0 — 5y
0
I've tested it and Position does fire on changed event KenUSM 53 — 5y
0
^ Well, it didn't fire for me. Did you have to manually move it or did you use an explosion to move it? User#20279 0 — 5y
0
I manually moved it but ill test it KenUSM 53 — 5y
0
I think it's because un-anchor doesn't change property. Fragmentation123 226 — 5y

2 answers

Log in to vote
1
Answered by
KenUSM 53
5 years ago

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.

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 User#20279 0 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

Answer this question