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

attempt to index upvalue 'Variable' (a nil value)?

Yo so question; got a basic script here:

local NPC = game.ReplicatedStorage.NPC
local customer = game.ReplicatedStorage.Events.Customer
local username = game.ReplicatedStorage.Events.Username
local green = game.Workspace.Green.Owner.Value
local red = game.Workspace.Red.Owner.Value
local Owner

username.OnServerEvent:Connect(function(player)
	if player == green then
		Owner = green
	elseif player == red then
		Owner = red
	end
end)

customer.OnServerEvent:Connect(function()
	if Owner.Value.."'s customer" == workspace:WaitForChild(Owner.Value.."'s customer") then
		print('exisiting')
	else
		print('new customer')
		local newNPC = NPC:Clone()
		newNPC.Parent = workspace
		newNPC.Name = Owner.Value.."'s customer"
		newNPC.Pathfinding.Disabled = false
		print('pathfinding enabled')
	end
end)

and its popin this error

attempt to index upvalue 'Owner' (a nil value)

The end goal is the create an NPC with a user's name and if an existing customer exists then it doesn't create a new one.

2 answers

Your doing

Owner.Value

when the Owner vairiable is set to the value. Hope this helps!

It really depends on which event fires first. It appears that the second event fires before the first one, which is why you're getting the error because Owner is still a nil variable. You can easily swap the order of when the events fire and it should fix your problem.

You're also doing Owner.Value when Owner is actually assigned to a number, not an Instance. Simply use Owner.

4 comments

0

Returning with attempt to " concatenate upvalue 'Owner' "

0

tostring(Owner)

0

Infinite yield possible on 'Workspace:WaitForChild("nil's customer")'

0

That's a different problem. That likely doesn't exist in the Workspace.

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