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

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

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

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)

3 comments

1

Instead of using a loop, try using a .Changed event. That would probably be more effective and causes less lag.

0

That is true, I'll rewrite an example of that.

0

I'd use script.Parent.BodyVelocity:GetPropertyChangedSignal('Velocity'):Connect(function() --// Code here end)

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