Attempt to index nil with 'Value'

Making a simple script that checks a folder to see if there is a value with a player name. If so it will delete the new car and keep the old one. But I have a problem:

attempt to index nil with ‘Value’

I made sure that the string value is there, and that the value is my name (Spiyder1). Not sure what the issue is, here’s the script that causes the issue (it is not the full script)

local function spawncar(player, cartosend)
	if cartosend == 'oldvan' then
		local dupe = game.ServerStorage.cars.oldvan:Clone()
		dupe.Parent = workspace.cars
		local playerid = Instance.new("StringValue")
		playerid.Value = player.Name
		playerid.Parent = dupe
		playerid.Name = "playername"
		local pos = workspace.buildings.oceansidepizza.parking.position.Position
		local ori = workspace.buildings.oceansidepizza.parking.position.Orientation
		dupe:MoveTo(pos)
		local children = workspace.cars:GetChildren()
		for i = 1, #children do
			if children.playername.Value == player.Name then
				dupe:Destroy()
			end
		end
	end
end

the line that is causing the issue is:

if children.playername.Value == player.Name then

here’s the exact error:

ServerScriptService.buycar:41: attempt to index nil with ‘Value’

3 Likes

could you do print(children.playername) before the for loop and tell me what it says?

1 Like

I think you mean to write

for i , car in pairs(children) do
        if car.playername.Value == player.Name then
		dupe:Destroy()
	end
end
1 Like

‘‘nil’’ (character minimum can get annoying)

that means the playername does not exist,

so I’d say this is the correct answer?

1 Like

Great! it works but when I try and spawn another car (to see if it works) there is an error:

The Parent property of oldvan is locked, current parent: NULL, new parent cars

Not sure what that means

that means the instance oldvan is deleted and you cannnot just put it back in the workspace

1 Like

Makes sense. But how would I be able to destroy the dupe before it goes into workspace. I made some changes to my script, but it doesn’t work and it doesn’t give any errors:

local function spawncar(player, cartosend)
	if cartosend == 'oldvan' then
		
		local dupe = game.ServerStorage.cars.oldvan:Clone()
		local children = workspace.cars:GetChildren()
		
		for i , car in pairs(children) do
			if car.playername.Value == player.Name then
				dupe:Destroy()
				dupe = nil
			end
		end
		
		if dupe == nil then
		else
			dupe.Parent = workspace.cars
			local playerid = Instance.new("StringValue")
			playerid.Value = player.Name
			playerid.Parent = dupe
			playerid.Name = "playername"
			local pos = workspace.buildings.oceansidepizza.parking.position.Position
			local ori = workspace.buildings.oceansidepizza.parking.position.Orientation
			dupe:MoveTo(pos)
		end
		
		
	end
end

why are you destoring the dupe you just made? it doesn’t really make sence to me

1 Like

I want to delete the car thats already in the workspace with the playername as the player trying to spawn the car. But I am not exactly sure how to do that. Figured I would put a teleport to car prompt if there is already a car that the player has spawned. Either that or just move it to the place where the dupes are being spawning

smooth brain moment: I just figured out that the script does what I want it to, not spawn a car when there is the car with the same playername already in the workspace. You can try to help with the deleting the car that was already spawned, but you don’t have to.

okay but why delete the dupe instead of the car?

for i , car in pairs(children) do
	if car.playername.Value == player.Name then
		dupe:Destroy()
		dupe = nil
	end
end
1 Like

How would I delete the car that was already in the workspace?

right now, you’re deleting the dupe each time you find a car with playername of let’s say “p1”.
so you have of corse, when you get to the if dupe == nil then dupe is already deleted, but the preexisting car stays the same, so there’s no error, and it seems like nothing has changed

Alright, but lets say if I want to delete the car that’s already in the workspace, how would I go about that?

you don’t seem to know what this pairs does so I’ll explain

local table = {"v1", "v2", "v3"};

for i , v in pairs(table) do
	--i = the position and v = the value
	print(i, v);
end

expected output:
1 v1
2 v2
3 v3

so in your case, for i , car in pairs(children) do i = the position the value is in, and car is the value
so do

car:Destroy();
2 Likes

thanks man. (character limit )