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

Why does my script give the error "attempt to index nil with 'name'"? [SOLVED]

This script was working fine like one month ago and now I'm back to this game and it gives me that error. What the script does is it checks if there is an exisiting car and if it does then deletes it and spawns another one.

for i,v in pairs(game.Workspace:GetChildren()) do
					if v.Name == game.Players.LocalPlayer.Name.."'s bmw" or v.Name == game.Players.LocalPlayer.Name.."'s mc" or v.Name == game.Players.LocalPlayer.Name.."'s por" or v.Name == game.Players.LocalPlayer.Name.."'s s13" or v.Name == game.Players.LocalPlayer.Name.."'s hoon" or v.Name == game.Players.LocalPlayer.Name.."'s gtr" then
                        v:Destroy()
					end
				end

3 comments

0

Are you executing this script in a LocalScript

0

No.

0

Where are you trying to execute this script? Is It within a player Character?

1 answer

If this script is not in a local script then you can not use 'LocalPlayer'. The best way to do this is probably with a RemoteEvent.

Try this.

Make sure to add a RemoteEvent to Replicated Storage.

Local Script:

local player = game.Players.LocalPlayer

for i,v in pairs(game.Workspace:GetChildren()) do
	if v.Name == player.Name.."'s bmw" or v.Name == player.Name.."'s mc" or v.Name == player.Name.."'s por" or v.Name == player.Name.."'s s13" or v.Name == player.Name.."'s hoon" or v.Name == player.Name.."'s gtr" then
		game.ReplicatedStorage.RemoteEvent:FireServer(v)
	end
end

Server Script:


game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, v)
	
	v:Destroy()
	
end)

4 comments

0

It stopped giving the error, but it's not deleting the car.

0

Oh really? It worked perfectly fine while I was testing it... Are you sure you did everything correctly?

0

Are you sure the Car's Name is correct? That's the only thing I can think that could be the issue. Because when I tested the code above, it worked fine. I added a part into workspace and I named it "xInfinityBear's bmw", and I tested it with all the names and they all worked.

0

I managed to fix it, I changed localplayer to the playeradded player parameter.

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