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

Disable a Localscript if BodyVelocity = 0,0,0?

Asked by 6 years ago

I've tried numerous scripts and they aren't working? Can anyone please help??

01function DisableDoors(player)
02    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.LeftDoors.LocalScript.Disabled = true
03    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.RightDoors.LocalScript.Disabled = true
04end
05 
06function EnableDoors(player)
07    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.LeftDoors.LocalScript.Disabled = false
08    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.RightDoors.LocalScript.Disabled = false
09end
10 
11if script.Parent.BodyVelocity.Velocity == Vector3.new(0, 0, 0) then
12    EnableDoors()
13else
14    DisableDoors()
15end

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

It is because the "if" statement only runs one time, consider putting it in a loop like this:

1repeat
2    if script.Parent.BodyVelocity.Velocity == Vector3.new(0,0,0) then
3        EnableDoors()
4    else
5        DisableDoors()
6    end
7    wait()
8until 1==0

Instead of:

1if script.Parent.BodyVelocity.Velocity == Vector3.new(0,0,0) then
2    EnableDoors()
3else
4    DisableDoors()
5end

which could only be ran once.

Edit (Suggested by Sarcadistic): As they mentioned, you could use a .Changed event:

1script.Parent.Changed:connect(function()
2    if script.Parent.BodyVelocity.Velocity == Vector3.new(0,0,0) then
3        EnableDoors()
4    else
5        DisableDoors()
6    end
7end)
1
Instead of using a loop, try using a .Changed event. That would probably be more effective and causes less lag. Sarcadistic 1 — 6y
0
That is true, I'll rewrite an example of that. Cvieyra2test 176 — 6y
0
I'd use script.Parent.BodyVelocity:GetPropertyChangedSignal('Velocity'):Connect(function() --// Code here end) DEVLogos 8 — 5y
Ad

Answer this question