[Solved] Attempt to Index Nil Value

I have a script that handles quests in my game. What it does is it handles two different arrays using an index. I already have various scripts that make the stats higher and handle that. The issue is that it’s not displaying on my screen because the script errors before even attempting to utilize a RemoteEvent.

Here is part of my stat & datastore script in ServerScriptService (this handles leaderstats and other things that are different for each player):

Note: I already defined plr, and this section works. It’s just shown for display purposes & for reference.

local questValue = Instance.new("IntValue") -- This stores the reward that will be given when the quest is completed
questValue.Name = "QuestValue"
questValue.Value = 0
questValue.Parent = plr
local questNum = Instance.new("IntValue") -- This is essentially the index that tells which quest to present to the player. I made this under the player to let various scripts access it with ease
questNum.Name = "QuestNum"
questNum.Value = 1
questNum.Parent = plr
local questCompleted = Instance.new("BoolValue") -- Tells the player whether or not the quest has been completed, they can click a button to cash out their rewards
questCompleted.Name = "QuestCompleted"
questCompleted.Value = false
questCompleted.Parent = plr

I have implemented datastore as well but did not show it here for brevity.

Here is my other script in ServerScriptService, called QuestHandler:

local player
    player = plr -- A sneaky script I use that lets me use the "player" parameter anywhere. It works on other scripts, so I don't think it's the problem

local questStrings = {
    -- A (relatively) long array of strings that are given to the player, I use this as reference to remember which quest requires what

local questRewards = {
    -- Also a slightly long array, just displays which rewards will be given for each quest

while true do
    local questNum = player.QuestNum --Error pops up here
    local currentQuestString = questStrings[questNum.Value]
    local currentQuestReward = questRewards[questNum.Value]
    game.ReplicatedStorage.DisplayQuest:FireClient(player, currentQuestString, currentQuestReward, player.QuestCompleted.Value)
    wait(0.1) -- I don't need it to activate ALL the time as long as it doesn't seem like it's lagging

And then there’s another LocalScript that uses OnClientEvent but I have verified it works. The issue is on the local questNum part because that’s the line that provides the error.

I’ve tried many solutions, such as defining global variables in game.Players.PlayerAdded, but errors have been popping up anyway. Please tell me if you understand what the issue is because it’s been bugging me (no pun intended) for a while.

1 Like

The player value is nil. The line that errored is ran before the player variable is assigned.


Okay I made some edits and it works, thanks!