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

why does if statements not work normally in characteradded Event?

so i have been using these line of code

local Players = game:GetService("Players").PlayerAdded Players:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Var == true then
			--Do something(note:this will always be nil and doesnt do anything)
		else
			--Do something else
		end
	end)
end)

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

1 comment

0

line 1 works perfectly by the way

2 answers

==== 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

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		-- my code
	end)
end)

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

local Players = game: GetService("Players").PlayerAdded

Players:Connect(function(Player)
	-- This is simplified code
end)

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)

local Var == true
local Players = game:GetService("Players").PlayerAdded 

Players:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        if Var == true then
            --Do something(Trigger this because it have detected variable and it's true)
        else
            --Do something else
        end
    end)
end)

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!

7 comments

0

If you found this solution helpful then you can accept this answer and help others who has the same problem.

0

im writing just some print statements inside ifs

0

by the way i was using getservice.playeradded because it works and roblox let it run

0

Oh, so you just didn't write all code. I will now rewrite my solution.

0

Answer has been rewritten, Rewritten answer is below the archived answer indicated by ==== REWRITTEN ANSWER ====

0

your answer might not have given me an answer but i think it gave me an idea

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.

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:

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

Another example:

--// Number based
local UserID = 158811 -- Your userId

local Players = game:GetService("Players").PlayerAdded Players:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.UserId == UserID then
			print("Hai Boss!")
			--Do something(note:this will always be nil and doesnt do anything)
		else
			print("Hello Stranger!")
			--Do something else
		end
	end)
end)

etc...

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

Hope this helps!

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