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

Nil value even though variable is already described?

Asked by 6 years ago

Hiya! In the script i have, I was trying to make an owner's only door. only problem is... when my script describes the variable of tycoon on line 14, on line 15 the script tells me it has a nil value. Help?

dad = script.Parent
door = dad.Parent
owner = door.Parent.Owner

local active = 0

function onTouched(part)
    local human = part.Parent:findFirstChild("Humanoid")
    if human ~= nil and active == 0 then
        active = 1
        local player = game.Players:findFirstChild(part.Parent.Name)
        if player ~= nil then
            if owner.Value == "" then
                local tycoon = player:findFirstChild("Tycoon") -- describes tycoon
                if tycoon.Value == 0 then -- where error eccours
                    tycoon.Value = 1
                    owner.Value = player.Name
                    local message = Instance.new("Message")
                    message.Text = "You now own this tycoon!"
                    message.Parent = player
                    wait(3)
                    door.Name = "Tycoon Owner: " .. owner.Value
                    message:remove()
                else
                    local message = Instance.new("Message")
                    message.Text = "You already own another tycoon!"
                    message.Parent = player
                    wait(3)
                    message:remove()
                end
            end
        end
    end
    active = 0
end

dad.Touched:connect(onTouched)

Here's what the output says.

10:00:49.307 - Workspace.MrNiceGuy's Beginner Tycoon Kit.Tycoon.This Tycoon has no owner..OwnerDoor.[MNG]-OwnerScript:15: attempt to index local 'tycoon' (a nil value)
0
your using "findfirstchild" which doesn't guarentee it will return an object, use "waitforchild" instead abnotaddable 920 — 6y
0
also its "FindFirstChild" not "findFirstChild" abnotaddable 920 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

maybe because the player isn't apart of the tycoon yet so instead do waitforchild so when it is there then maybe it'll work or maybe the player is already in the tycoon then idk

Ad

Answer this question