I have an if statement that looks perfectly fine to me but keeps coming up with errors once it runs. It is checking a leaderstat value in the player and checking its value.
Code:
if player:FindFirstChild("OwnsTycoon").Value == false then
There is an end
for the if statement, it’s just later on in the script but that one line above is the one coming up with the error.
Error:
Edit:

It does go under the player correctly and below is the code to make it.

I don’t understand what is happening but if you could help it would be appreciated!
Looks like that line isn’t finding “OwnsTycoon” and thus will error because nil.Value will not exist
you could try making it WaitForChild instead, if all your players will have that instance
Usually whenever this happens, I try to check if the item that you’re trying to get the value of exists in that area. So in this case, “OwnsTycoon” might not be parented under player, or maybe you spelled it wrong.
Comes back with an Infinite Yeild.
If it’s stating inf. yeild then that instance hasn’t shown up yet (it’s waiting for something to create it), so maybe your if statement should instead state…
if player:FindFirstChild(“OwnsTycoon”) and player.OwnsTycoon.Value == false then
The instance you’re looking for probably doesn’t and won’t exist. Try to check your code to see if “OwnsTycoon” really gets instanced under player.
You are getting the error because player:FindFirstChild("OwnsTycoon")
is returning nil. A better way to do this is this:
local ownsTycoon = player:FindFirstChild("OwnsTycoon")
if ownsTycoon ~= nil then
if ownsTycoon.Value == false then
-- Do whatever here
end
end
This way, if FindFirstChild
doesn’t find it for whatever reason, the script won’t fail on an error. If the value is expected, you could have a race condition. In that case, try using WaitForChild
instead.
Hope this helps.
I made an edit to my topic if you want to look back at it and I tried your code snippet and nothing happens when I run it.
Is this in a (Server)Script or LocalScript?
Actually better question: Is the player variable taking in count for the leaderstats folder?
if player.leaderstats:FindFirstChild("OwnsTycoon").Value == false then
I don’t believe you’re calling the leaderstats folder.
Edit: Nevermind, I’m dumb, you are, I didn’t see the picture below it.
So with the update to your main post we can see that your OwnsTycoon instance is inside the Leaderstats folder, not inside player.
FindFirstChild by default doesn’t look inside other children, so it wont look in leaderstat, instead you can specify
if player.leaderstats:FindFirstChild(“OwnsTycoon”) and player.leaderstats.OwnsTycoon.Value == false then
That changes things. I was not aware that this is part of your leaderstats. I haven’t messed with leaderstats enough to give you a definitive answer. However, my code should work. Make the following changes:
local ownsTycoon = player:FindFirstChild("OwnsTycoon")
print("OwnsTycoon", ownsTycoon)
if ownsTycoon.Value ~= nil then
print("OwnsTycoon.Value:", ownsTycoon.Value)
if ownsTycoon == false then
-- Do whatever here
end
end
The first print statement will print nil if ownsTycoon is nil. If it’s not nil, then it will print the name. The second print statement will print the value if it’s not nil. This will give you some diagnostics to see where the problem is.
Is the value actually false when you run it?
(You forgot to add .Value
to your if statement)
1 Like
Yeah, I missed the .Value on the second if statement. Thanks to @AxBoy_DEV for pointing that out.
1 Like
I figured out what it was.
I was putting in the player’s leader stats instead of in the player itself. The if statement was trying to find the value in the player and not the leader stats folder thus why it returned nil.
Thanks to everyone who tried helping me with this problem, it was a very easy solution, just had to change the location.
3 Likes