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

Attempt to index nil with 'WaitForChild' ?

Hello, im new to scripting and this is my first game im trying to make...

what im trying to achieve is when the player doesn't have enough money, the button stays red and if the player have enough money, the button turns green. FYI im making a tycoon game.

local tycoon = script.Parent.Parent.Parent.Parent
local owner = tycoon.TycoonOwner.Value
local button = script.Parent.BuyButon


local function CanBuy(player)
	local pStats = player:WaitForChild("leaderstats")
	local money = pStats:FindFirstChild("Money")
	
	
	if player then
		if owner.Name == player.Name then
			if money.Value >= script.Parent.Cost.Value then
				button.BrickColor = BrickColor.new("Bright green")
			else
				button.BrickColor = BrickColor.new("Bright red")
			end
		end
	end
end

	while true do
		CanBuy()
		wait(0.5)
	end

6 comments

0

What line is the error at?

0

Do you have leaderboards setup yet?

0

in line 07

0

yes, i set up the leaderboards

0

my guess: since you are firing the CanBuy function with a while true do, its going to run into an error maybe because it fires before the leaderboard actually loads. Just a guess though

0

hmm the error is gone, but the script not working

1 answer

✓ Accepted answer

Player is not defined when firing function

local tycoon = script.Parent.Parent.Parent.Parent
local owner = tycoon.TycoonOwner.Value
local button = script.Parent.BuyButon
local player = game:GetService("Players").LocalPlayer -- define player at the beginning

local function CanBuy()
    local pStats = player:WaitForChild("leaderstats")
    local money = pStats:FindFirstChild("Money")


    if player then
        if owner.Name == player.Name then
            if money.Value >= script.Parent.Cost.Value then
                button.BrickColor = BrickColor.new("Bright green")
            else
                button.BrickColor = BrickColor.new("Bright red")
            end
        end
    end
end

    while true do
        CanBuy()
        wait(0.5)
    end

2 comments

1

the error is gone, but the script is not doing what it intended to

0

Maybe there is no owner and the script doesnt run completly

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