Variable Inside Function Isn't Working (Tried Many Solutions, But It Still Isn't Working)

Hello! I am trying to make a script that performs a function when a player touches a part, but this error occurs and I have been trying to solve it for days:

idontknow

local tributegui = game:GetService("ServerStorage"):FindFirstChild("DistrictGUI")
local workspace = game:GetService("Workspace")
local players = game:GetService("Players")
script.Parent.Touched:Connect(function(ez)
	local ggplayer = players:GetPlayerFromCharacter(ez)
	local body = ez.Parent
	local checks = ggplayer:FindFirstChild("DistrictChecks")
	if checks.hasguiequipped.Value == false then
		if ggplayer then
			local tgui = tributegui:Clone()
			tgui.TextLabel.TextColor3 = Color3.fromRGB(213, 213, 16)
			tgui.Parent = game.Workspace:WaitForChild(ggplayer.Name).Head
			checks.hasguiequipped.Value = true
			checks.D1.Value = true
		end
	else
		if checks.D1.Value == false then
			body.Head.DistrictGUI:Destroy()	
			if ggplayer then
				local tgui = tributegui:Clone()
				tgui.TextLabel.TextColor3 = Color3.fromRGB(213, 213, 16)
				tgui.Parent = game.Workspace:WaitForChild(ggplayer.Name).Head
				checks.hasguiequipped.Value = true
				checks.D1.Value = true
				checks.D2.Value = false
				checks.D3.Value = false
				checks.D4.Value = false
				checks.D5.Value = false
				checks.D6.Value = false
				checks.D7.Value = false
				checks.D8.Value = false
				checks.D9.Value = false
				checks.D10.Value = false
				checks.D11.Value = false
				checks.D12.Value = false
			end
		end
	end	
end)
1 Like

The ez variable passed by the touch function is a part inside the character not the character itself.
You will need to do players:GetPlayerFromCharacter(ez.Parent) since it required the character not the parts inside of it.
Or just move the local body = ez.Parent above it and just do players:GetPlayerFromCharacter(body)

You should also put the if ggplayer then check above anything that is trying to use ggplayer in case it gets touched by a part that isn’t a player.

You can use a loop for the district checks too so you don’t have to make all those lines.

for i,v in ipairs(checks:GetChildren()) do
  if v.Name == "D1" then
      v.Value = true
  else
      v.Value = false
  end
end

I changed the variables to:

	local body = ez.Parent
	local ggplayer = players:GetPlayerFromCharacter(body)
	local checks = ggplayer:FindFirstChild("DistrictChecks")

However, it says this in the output:
prrooof2

DistrictChecks is a part of the player when it joins.
prrooof

So the problem is that ggplayer is nil. So when you are getting your player from character something is wrong with using ez, or body. can you explain what ez is? it’s supposed to a player for it to work correctly.

you are just getting a player from a literal part and a part doesnt have any relations with the model thats the same path as the player’s character property so change ez to ez.Parent on line 5

The script is inside the part I am standing on in the picture below. The function is supposed to activate when a player touches the part. I intended for “ez” to represent the thing touching the part. (I named it “ez” because I thought that this script was going to be easy.
standingonpart

I am trying to access a folder inside a player when that player touches a part.

to:

local ggplayer = players:GetPlayerFromCharacter(ez.Parent)

Thanks for the suggestion. I tried moving body above ggplayer and doing what you described. Should I keep it like this?

local body = ez.Parent
local ggplayer = players:GetPlayerFromCharacter(body)
local checks = ggplayer:FindFirstChild("DistrictChecks")
1 Like

you can do that too but me personally i would make it more compact

1 Like

Ok maybe try printing ggplayer and ez just to see what comes up

I added the two prints and tested them out.

	local ggplayer = players:GetPlayerFromCharacter(ez.Parent)
	local body = ez.Parent
	print(ggplayer)
	print(ez)
	local checks = ggplayer:FindFirstChild("DistrictChecks")

With this, the moment I stood on the part, it started printing my username and the part of my avatar touching the part. When I clicked on my username in the output, it lead to my user in the “Players” section, which contains the “DistrictChecks” folder.
ezandwes

leadsto

Therefore, the issue must be in the checks variable.

`

Your script is obviously offended by the fact that you put “ez” in it, hence the errors it throws up.

The reason that your script is throwing up an error like that is because self of the inherited FindFirstChild() method is nil. This happens because you didn’t define what the character is in your script, you just gave it a basepart and expected it to work with it. Instead, you should do

script.Parent.Touched:Connect(function(ez)
 if not ez.Parent:FindFirstChild("Humanoid") and players:GetPlayerFromCharacter(ez.Parent.Name) == nil then return end --self explanatory 

 local ggplayer = players:GetPlayerFromCharacter(ez.Parent) --Get the player from the basepart's parent since we know it's a player

--other code
end)

this is correct, but using == nil is not recommended. studio also tells you that

The reason I put that is because Roblox’s if-statements are quite confusing to me, unlike the ones in C or C++. For example

local a = 5
local b = 6

if not a == 5 and b == 2 then
 print("hello") --prints
end

Which is why I deem C++ as a better language

Thanks for the suggestion and explanation. I added:

if not ez.Parent:FindFirstChild("Humanoid") and players:GetPlayerFromCharacter(ez.Parent.Name) == nil then return end

However, this error in the output occurs:
objectvalue

Can you show us the hierarchy of the player?

In the Workspace:
intheworkspace

In the Players section:
intheplayersection

Try

if not ez.Parent:FindFirstChild("Humanoid") or not players:GetPlayerFromCharacter(ez.Parent) then return end
1 Like

Then please replace it with

if not ez.Parent:FindFirstChild("Humanoid") and not players[ez.Parent.Name] then return end