Unsure why player is nil

  1. What do you want to achieve? Keep it simple and clear!

A flashbang, that when thrown, spawns a part (a hitbox) that applies a gui to any characters screen that it touches

  1. What is the issue? Include screenshots / videos if possible!
    The script doesn’t seem to know what “player” is. :sad:

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried adding an “if” statement that checks if player even exists. As expected, the flashbang function
I have also tried putting my code in a local script instead of a server script,

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Script below:

	local AoE = script.Parent -- the hitbox that is spawned when you throw the flashbang.
	local starterGui = game:GetService("StarterGui")
	local gui = starterGui.FlashbangScreen:Clone() -- gui consisting of a frame with a white background.

	local players = game:GetService("Players")

	


	local function flashbang(touchingPart) 
		local humanoid, touchingCharacter = u.findHumanoid(touchingPart) --findHumanoid is a function in the module utility.

		if humanoid and touchingCharacter and humanoid.Health > 0 then
    			local player = players:GetPlayerFromCharacter(touchingCharacter)
				gui.Parent = player.PlayerGui -- this errors.
				local count = 0
				while count < 21 do -- a while loop.

					wait(.1)
					count += 1
					gui.Frame.BackgroundTransparency += .05 -- making the gui disappear over time.
				end
		end
	

	end

	local function onTouch(partTable) 

		if #partTable > 0 then		
			for i,v in pairs (partTable) do
				if v.Name == "HumanoidRootPart" then -- if there's a humanoidrootpart touching AoE, then run the flashbang function.
					flashbang(v)
				end
			end
		end
		
	end

		local partTable = workspace:GetPartsInPart(AoE) -- a table of what parts are touching the AoE
		if #partTable > 0 then
			onTouch(partTable) -- if there are parts touching AoE, run onTouch
		end

Obviously, please do not respond with deprecated code. I am also not looking for ChatGPT answers.

Thank you for your help/time.
Cheers! :happy3:

From a quick look I do not see anything wrong so it might have somethign to do with how ur game work / where everything located. When I do see errors in my game / things that does not work often times the code is correct and the problem is in the game. So I’d take a look and see if everything’s placed and fucntioning correct.

What’s the error saying exactly? Also, have you tried printing the player? If it is nil, resort to the root issue which would be this line:

local humanoid, touchingCharacter = u.findHumanoid(touchingPart) --findHumanoid is a function in the module utility.```

oops wrong person sorry
asdfghjkl

could you please try printing the touchingCharacter variable to see if it returns nil, or an object that makes GetPlayerFromCharacter() return nil

I had it print I believe a few lines above the line marked with “this errors”. What was weird is my print just. didn’t. print.
Nothing in server output, and while it wouldn’t make sense if there was, nothing in client output.

Thanks for your response.

The error says something along the line of “Trying to index PlayerGui with nil”.