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

Why won't my Velocity script work?

Hello! I am making a blackout script for my plane game. If the player goes 210 studs downward, a frame in the StarterGUI will turn visible. Which in this case is a vignette simulating a blackout. When the player reaches velocity under 210, it will go away. I have the script inserted into the player already, I just need help fixing and enhancing this.

In the output it says: 09:25:50.359 - Workspace.Player.LocalScript:4: 'then' expected near '='

This is what I have so far:

s = script.Parent


if game.Workspace.Humanoid.Torso.Velocity = Vector3.new(0,-210,0) then
game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = true

if game.Workspace.Humanoid.Torso.Velocity = Vector3.new(0,-209,0) then
game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = false

4 comments

0

You need two equal signs look at my answer.

0

==(line 4)

0

@dragonmaster4111 why did you Open a Claim and said this post is a spam?

0

@dragon this is not a spam post

4 answers

✓ Accepted answer

ErrorType: Syntax

if Statement With Operator

In an 'if' statement, you must have '==' if you want a value equal to another value. But in your case, use '<=' to find see if the velocity is less than or equal to the value.

if game.Workspace.Humanoid.Torso.Velocity <= Vector3.new(0,-210,0) then

The way you had set up your 'if' statement can be wonky, as the format looks like this:

if game.Workspace.Humanoid.Torso.Velocity <= Vector3.new(0,-210,0) then
	game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = true
	if game.Workspace.Humanoid.Torso.Velocity >= Vector3.new(0,-209,0) then
		game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = false
	end		-- Assuming you had 'ends'
end

if Statements

If the velocity is (0,-210,0), then there is no way that the second 'if' statement will run.

Separate your 'if' statements.

if game.Workspace.Humanoid.Torso.Velocity <= Vector3.new(0,-210,0) then
	game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = true
end
if game.Workspace.Humanoid.Torso.Velocity >= Vector3.new(0,-209,0) then
	game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = false
end	

ErrorType: Logic

New/ Forgotten Materials

Since you probably would want the script to check the torso's velocity all the time, you will want to place the script in a 'while' loop.

s = script.Parent

while true do
	if game.Workspace.Humanoid.Torso.Velocity <= Vector3.new(0,-210,0) then
		game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = true
	else	-- Added an 'else'; if the Torso's velocity is not less than (0, -210, 0), then the GUI will be invisible; it works almost the same as the two 'if' statements way
		game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = false
	end
	wait(.1)
end

Unintended Error

game.Workspace.Humanoid.Torso... indicates that if the 'Humanoid' object in 'Workspace' specifically has a velocity of (0,-210,0). This means that all of the players' BlackoutGUI will be visible, which is kind of flawed if other players aren't in the same plane.

if game.Players.LocalPlayer.Character:FindFirstChild("Torso").Velocity == Vector3.new(0,-210,0) then

Vector3 - Velocity

There's another problem. The above is saying that if the Torso has a velocity of (0,-210,0) and only (0,-210,0), then the GUI will be visible. Unfortunately, if it's not, then as indicated by 'else' (or your extra 'if' statement), the GUI will become invisible.

How is this a problem? Well, for one, your plane will move forward (more or less), which makes your Torso's velocity have the x- and/or z-axes higher/lower than 0. To solve this problem:

local CurrentXValue = game.Workspace.Humanoid.Torso.Velocity.x
local CurrentZValue = game.Workspace.Humanoid.Torso.Velocity.x
-- while loop open code block
if game.Players.LocalPlayer.Character:FindFirstChild("Torso").Velocity <= Vector3.new(CurrentXValue,-210,CurrentZValue) then-- Uses the Torso velocity's x- and z-axes, instead of the limiting '0'.

2 comments

0

Okay, I am REALLY CONFUSED. I understand that Ineed to put an == after an if. But I don't know exactly where I am supposed to put all this stuff together. You just gave me abunch of things then told me their errors and i understand that, But then you added the other two things to fix them but I am not sure where to put them. Can you put it all together please?

0

Updated, but I can not compile all of the elements, that I just talked about, together for you.

Ahhh, this problem is the bane of programmers' existance.

Here's the issue. The = symbol is called the assignment operator. You use it to say stuff like

a = script.Parent
b = 5
c = true

etc.

But here, you want to check if two things are the same; namely the player's velocity and a specific velocity. We are used to using an = for stuff like that because that's what we do in math; but that's not how it works with computers. In programming, if you are checking if two things are equal to each other, such as in an if statement or loop, you need to use a ==. Here's some examples...

a = 5 --Assignment
b = 4 --Assignment

if(a == 5) then
	print("A is 5")
end

if(a ~= b) then
	print("A is not the same as B")
end

Notice in the second example I used ~=. That means "not equal to."

On a side note, I'm not sure how LUA handles what you are doing(Trying to assign nonassignable variables inside an if statement) but I know that C++ returns true or false if you do that, so the if statement will almost always happen.

On a second note, you want to check if the player is above or below certain points. If the player isn't exactly at those speeds when the script runs, it won't change what's happening. For example, the player could have a velocity of (1, -210, 0) just slightly moving forward, and the vignette wouldn't start. What I would suggest is comparing just the y part of the player's speed. Something along the lines of this...

s = script.Parent
if game.Workspace.Humanoid.Torso.Velocity.y < -210 then
	game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = true

if game.Workspace.Humanoid.Torso.Velocity.y >-210 then
	game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = false

This way, it doesn't matter if the player is moving forward or sideways, and if the script doesn't run at the exact same game tick as the player crosses the -210 threshold, it'll still change the vignette

2 comments

0

Lua avoids dangerous behavior like assignment evaluating to something -- it will just give a syntax error. Also, sidebar: = does not evaluate to true in C. It evalutes to the new value of the variable, hence, a = 0 would evaluate to 0 and act as "false" in C.

0

Ahhh ok; I'm new to C/C++ this semester, and I knew that it didn't do the same thing as LUA, and I wasn't super concerned about getting it correct because this is a LUA post, not C/C

You gotta put two equal signs if your trying to see if its equal. == You forgot to run the script somehow.

For example:

while true do
brick = 3
if brick == 3 then
print("brick is three")
end

or if you wanna say not equal to:

while true do
brick = 4
if brick ~= 3 then
print("brick does not equal to three")
end

you can use these to say greater than or less than: <=, >=

Now for the answer:

s = script.Parent

if game.Workspace.Humanoid.Torso.Velocity == Vector3.new(0,-210,0) then
game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = true

if game.Workspace.Humanoid.Torso.Velocity == Vector3.new(0,-209,0) then
game.Players.LocalPlayer.PlayerGui.BlackoutGUI.Frame.Visible = false

just make the "if this = this then" into "if this == this then" i see "=" as becomes, "==" means equal to

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