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:
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)
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
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.
I am trying to access a folder inside a player when that player touches a part.
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.
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)