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

Attempt to index nil with 'WaitForChild' ?

Asked by 3 years ago

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
0
What line is the error at? User#41156 0 — 3y
0
Do you have leaderboards setup yet? iamajunky 20 — 3y
0
in line 07 Struggage 10 — 3y
0
yes, i set up the leaderboards Struggage 10 — 3y
View all comments (2 more)
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 speedyfox66 237 — 3y
0
hmm the error is gone, but the script not working Struggage 10 — 3y

1 answer

Log in to vote
0
Answered by
sayer80 457 Moderation Voter
3 years ago
Edited 3 years ago

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
1
the error is gone, but the script is not doing what it intended to Struggage 10 — 3y
0
Maybe there is no owner and the script doesnt run completly sayer80 457 — 3y
Ad

Answer this question