Attempt to index nil with 'Parent'

Hey fellas.

I’m having problems with GetPlayerFromCharacter on a bass script that is supposed to move subs with the bass in an audio ID, It works as intended, however it throws lots of errors and lags the game more than necessary

This is what the workspace looks like, it is a plugin for the A chassis 1.5 if any of you are familiar with it.
image

I’ve looked all over the dev forums and YouTube, but people with similar errors aren’t explaining it too well, or the fix that worked for them doesn’t translate to this code.

First code is the BassMod script

if script.Parent:IsA("VehicleSeat") then
	script.Parent.ChildAdded:connect(function(child)
		if child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
			local p= game.Players:GetPlayerFromCharacter(child.Part1.Parent)
			local g= script.SendBass:Clone()
			g.Parent=p.PlayerGui
			g.src.Value = script.BassEvent
			g.Disabled = false	
		end
	end)
	script.Parent.ChildRemoved:Connect(function(child)
		if child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
		end
	end)
end

script.BassEvent.OnServerEvent:Connect(function(plr, bass)
	local car = script.Parent.Parent
	local bassParts = car.Body.BassParts
	for i,v in pairs(bassParts:GetChildren()) do
		if v:IsA("MeshPart") then
			local vectorSize = v:FindFirstChild("VectorSize")
			if bass <= v.MaxBassSize.Value then
				v.Size = Vector3.new(vectorSize.Value.X,bass/v.BassSize.Value,vectorSize.Value.Z)
			else
				v.Size = Vector3.new(vectorSize.Value.X,v.MaxBassSize.Value/v.BassSize.Value,vectorSize.Value.Z)
			end
		end
	end
end)

The second is the Control script.

if script.Parent.Parent:IsA("VehicleSeat") then
	script.Parent.Parent.ChildAdded:connect(function(child)
		if child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
			local p=game.Players:GetPlayerFromCharacter(child.Part1.Parent)
			local g=script.SongGui:Clone()
			g.Parent=p.PlayerGui
			g.src.Value = script.Parent
			g.SongHandeler.Disabled=false	
		end
	end)
	script.Parent.Parent.ChildRemoved:Connect(function(child)
		if child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
		local p=game.Players:GetPlayerFromCharacter(child.Part1.Parent).PlayerGui.SongGui:Destroy()
		end
	end)
end

script.Parent.SongEvent.OnServerEvent:Connect(function(plr, PlayOrStop,song)
	if PlayOrStop == "Play" then
		script.Parent.SoundId = "rbxassetid://" .. song
		script.Parent:Play()
	else
		if PlayOrStop == "Stop" then
			script.Parent:Stop()
		else
			if PlayOrStop == "SetVolume" then
				script.Parent.Volume = song
			end
		end
	end
end)

both scripts are throwing the same error "Attempt to index nil with ‘Parent’ both referring to the third line in each script. I am a beginner so any help would be appreciated in a basic form.

You are getting the character incorrectly, you should use Occupant to get the character like so:

script.Parent.ChildAdded:connect(function(child)
	if child:IsA("Weld") then
		local hum = script.Parent.Occupant
		local player = game.Players:GetPlayerFromCharacter(hum.Parent)
		print(player.UserId)
	end 
end)

The above is just an example

1 Like