Index nil with parent

Hey there,

This is probably a very simple issue, but i cant find anyway to fix it. I have tried searching the devforum and fixing it myself but had no luck.

So basically the script is within a part, so i should be able to do something such as:

script.Parent.Touched:Connect(function()

But i can’t. Im not too sure what the issue is. It could be because its being called from within a function but not 100% sure.

Here is the script:

local repStore = game.ReplicatedStorage
local events = repStore.Core.Events

local function addAtmosphere(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- Issue occurs here saying: attempt to index nil with 'Parent'
		return
	end
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	events.AtmosphereAdd:FireClient()
end

script.Parent.Touched:Connect(function()
	addAtmosphere()
end)

The script is inside the part and is a server script.

Thanks for the help! :slight_smile:

1 Like

Are you getting any errors? And what exactly is the issue that occurs.

Oh I realized the issue! the hit variable is nil because it must be tied to the touched function like so.

script.Parent.Touched:Connect(function(hit)
	addAtmosphere(hit)
end)
1 Like

@yoolurs @rtvr56565

Thank you both for the help, but i am now getting a new issue:

Argument 1 missing or nil

Turn it into:

events.AtmosphereAdd:FireClient(player)
1 Like

Thank you for the help.

But i am getting an error saying that the player must be a player object.

FireClient: player argument must be a Player object

oh yeah to fire to a client u must specify the player u are firing to. If you wish to fire to all clients use :FireAllClients, otherwise do :FireClient(plr)

1 Like

I only want to fire the client that touches said part.

I had already set this and got the error.

I dont know why its being so weird.

Hello, the problem is that you’re not firing the event to a Player and you’re returning the function when actually it finds a Humanoid. This should fix it:

local Players = game:GetService("Players")
local repStore = game:GetService("ReplicatedStorage")
local events = repStore.Core.Events

local function addAtmosphere(hit)
	if hit then
		if hit.Parent:FindFirstChild("Humanoid") then -- Issue occurs here saying: attempt to index nil with 'Parent'
			local player = Players:GetPlayerFromCharacter(hit.Parent)
			events.AtmosphereAdd:FireClient(player)
		end
	end
end

script.Parent.Touched:Connect(addAtmosphere)
1 Like

Hello,

This has fixed the problem and currently i am not getting any errors, but the client script (local script) doesnt seem to be receiving the event when its fired. I tried adding a print event and the text was in the output.

Is this something to do with the local script or the server side?

If you want i can show you the client script

Have you tried printing when the server script fires the RemoteEvent? Also yes, it would be easier if you can send the LocalScript :slight_smile: .

1 Like

Yes, the second that the event is fired, it will print (“Event Received”).

Here is the script:

local repStore = game.ReplicatedStorage
local events = repStore.Core.Events

events.AtmosphereAdd.OnClientEvent:Connect(function()
	print("Event Received")
	game.Lighting.Atmosphere.Density = 0.642
	game.Lighting.Atmosphere.Glare = 1.67
	game.Lighting.Atmosphere.Haze = 2.5
end)

Here I meant on the server script printing just after the :FireServer().

Also, where is the LocalScript located?

1 Like

this should be in the server side

local repStore = game:GetService("ReplicatedStorage") // getting the RS service
local events = repStore.Core.Events

local function addAtmosphere(hit)
	if not (hit and (hit.Parent) and (hit:FindFirstChild("Humanoid"))) then
		return
	end
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	events.AtmosphereAdd:FireClient(player, "DABABY LESS GOOO")
end

script.Parent.Touched:Connect(addAtmosphere)
1 Like

The local script is located within the part in which the server script is.

Ah, i though you meant when the client receives the event.

Yep, that’s what I thought. LocalScripts do not run on Workspace, try changing its location to StarterPlayer > StarterPlayerScripts

1 Like

Just out of curiosity, what is the “DABABY LSS GOOO” argument used for?

Sorry if its something simple, i dont usually work with creating functions.

idk tbh

1 Like

That script does the exact same thing as mine, but using return. “DABABY LESS GOOO” is just an argument that he passed.

1 Like

Oh, my mistake, thank you for clearing this up.