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

why does if statements not work normally in characteradded Event?

Asked by 4 years ago
Edited 4 years ago

so i have been using these line of code

1local Players = game:GetService("Players").PlayerAdded Players:Connect(function(Player)
2    Player.CharacterAdded:Connect(function(Character)
3        if Var == true then
4            --Do something(note:this will always be nil and doesnt do anything)
5        else
6            --Do something else
7        end
8    end)
9end)

and i notice that if statements here just dont work normally like its suppose to in them

can anybody explain why?

also im not asking the code to be fixed

0
line 1 works perfectly by the way proROBLOXkiller5 112 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

==== WARNING! THIS IS ARCHIVED ANSWER =====

I am not sure about the code you're writing and I don't know how to explain the solution. Anyway here's the code to fix

Solution Code

1local Players = game:GetService("Players")
2Players.PlayerAdded:Connect(function(Player)
3    Player.CharacterAdded:Connect(function(Character)
4        -- my code
5    end)
6end)

You can't save an event in a variable and then call it later like the code below

1local Players = game: GetService("Players").PlayerAdded
2 
3Players:Connect(function(Player)
4    -- This is simplified code
5end)

I don't know why but it just happens that you can't save an event into a variable and then make a variable (event) connect function.

Happy Scripting!

==== REWRITTEN ANSWER ====

You are trying to check something that never exists. It's possible that you never declared the variable which when the script tries to check then it will return nil because the variable never exists.

Fixed Code (Note: Using Var as true)

01local Var == true
02local Players = game:GetService("Players").PlayerAdded
03 
04Players:Connect(function(Player)
05    Player.CharacterAdded:Connect(function(Character)
06        if Var == true then
07            --Do something(Trigger this because it have detected variable and it's true)
08        else
09            --Do something else
10        end
11    end)
12end)

In the future, you may want to try and check if the things you are checking actually exists if it never exists then it will return nil. You can even use if var == nil then and it will trigger this condition.

Happy Scripting!

0
If you found this solution helpful then you can accept this answer and help others who has the same problem. Somone_exe 224 — 4y
0
im writing just some print statements inside ifs proROBLOXkiller5 112 — 4y
0
by the way i was using getservice.playeradded because it works and roblox let it run proROBLOXkiller5 112 — 4y
0
Oh, so you just didn't write all code. I will now rewrite my solution. Somone_exe 224 — 4y
View all comments (3 more)
0
Answer has been rewritten, Rewritten answer is below the archived answer indicated by ==== REWRITTEN ANSWER ==== Somone_exe 224 — 4y
0
your answer might not have given me an answer but i think it gave me an idea proROBLOXkiller5 112 — 4y
0
If you have found answer which may not be directly then you can accept this answer and help others who are having same problem. Somone_exe 224 — 4y
Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago

Not sure what you're asking as your Var is nil in this example so asking if nil == true won't work as nil is actually false or nothing

to properly use an if statement you need to compare non nil things for example the following code checks to see if the player that joined is named TGazza and if so then print out a hello otherwise, it will print out hello stranger..

Code:

01--// String based!
02local Admin = "TGazza" -- change this to your name, has to match exactly!
03-- Ie tGazza wont work nor tgazza !!
04local Players = game:GetService("Players").PlayerAdded Players:Connect(function(Player)
05    Player.CharacterAdded:Connect(function(Character)
06        if Player.Name == Admin then
07            print("Hai Boss!")
08            --Do something(note:this will always be nil and doesnt do anything)
09        else
10            print("Hello Stranger!")
11            --Do something else
12        end
13    end)
14end)

Another example:

01--// Number based
02local UserID = 158811 -- Your userId
03 
04local Players = game:GetService("Players").PlayerAdded Players:Connect(function(Player)
05    Player.CharacterAdded:Connect(function(Character)
06        if Player.UserId == UserID then
07            print("Hai Boss!")
08            --Do something(note:this will always be nil and doesnt do anything)
09        else
10            print("Hello Stranger!")
11            --Do something else
12        end
13    end)
14end)

etc...

Link to a Roblox Lua tutorial on Youtube from Roblox: https://www.youtube.com/watch?v=0LiaEDui2vE

Hope this helps!

Answer this question