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’
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
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.
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