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 5 years ago

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

function DisableDoors(player)
    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.LeftDoors.LocalScript.Disabled = true
    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.RightDoors.LocalScript.Disabled = true
end

function EnableDoors(player)
    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.LeftDoors.LocalScript.Disabled = false
    player.PlayerGui.TrainDriveGui.Screen.JunctionsTab.RightDoors.LocalScript.Disabled = false
end

if script.Parent.BodyVelocity.Velocity == Vector3.new(0, 0, 0) then
    EnableDoors()
else
    DisableDoors()
end


1 answer

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

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

repeat
    if script.Parent.BodyVelocity.Velocity == Vector3.new(0,0,0) then
        EnableDoors()
    else
        DisableDoors()
    end
    wait()
until 1==0

Instead of:

if script.Parent.BodyVelocity.Velocity == Vector3.new(0,0,0) then
    EnableDoors()
else
    DisableDoors()
end

which could only be ran once.

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

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

Answer this question