Touch Script Detection Issues

What is wrong with my script it keeps giving me this output I am trying so hard to save the username so this doesn’t happen and keeps happening.

image

local buttonPressed = false

script.Parent.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")	
		if hit.Parent:FindFirstChild("Bag") ~= nil then	
				game.ReplicatedStorage.Events.CilentEvent:FireClient(game.Players:FindFirstChild(human.Parent.name), "StinkyBag")
				buttonPressed = true
				script.Parent.Sound:Play()
				game.ReplicatedStorage.PlayerSettings.Animation[human.Parent.Name].Value = "None"
				game.ReplicatedStorage.PlayerSettings.ItemUsed[human.Parent.Name].Value = false
				game.Players[human.Parent.Name].PlayerGui.Menus.Options.RemoveSB.Visible = false
				workspace[human.Parent.Name].StinkyBag:Destroy()
				local OutputText = game.ReplicatedStorage.OutputText:Clone()	
				OutputText.TextColor3 = Color3.fromRGB(0, 255, 0)
				OutputText.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
				OutputText.Text = "Successfully took out used diaper bag."
				OutputText.LocalScript.Disabled = false
				OutputText.Parent = game.Players[human.Parent.Name].PlayerGui:FindFirstChild("Menus").Outputs
				local OutputText2 = game.ReplicatedStorage.OutputText:Clone()	
				OutputText2.TextColor3 = Color3.fromRGB(74, 225, 109)
				OutputText2.TextStrokeColor3 = Color3.fromRGB(41, 126, 60)
				OutputText2.Text = "+30 Littlebucks"
				OutputText2.LocalScript.Disabled = false
				OutputText2.Parent = game.Players[human.Parent.Name].PlayerGui:FindFirstChild("Menus").Outputs
				--game.Players[human.Parent.Name].leaderstats.Littlebucks.Value = game.Players[human.Parent.Name].leaderstats.Littlebucks.Value + 30
				wait(4.75)
				buttonPressed = false
	end
end)

From my vision, it is telling you that the humanoid has no parent because it is nil. Make sure to use an if statement to check if the humanoid actually exists.

if human then
    --Rest of the code here.
end

The problem here is that .Touched doesn’t only fire when a character touches it, but also with other parts. Therefore, other parts don’t have the Humanoid object.

Here’s another tip: Use FindFirstChildOfClass(“Humanoid”) or FindFirstChildWhichIsA(“Humanoid”), so it’ll find a humanoid by it’s type, not name. That may be useful in cases that the name of the Humanoid is not Humanoid. I’ve seen people name their humanoid “Human” or “Zombie” for example.
Instance:FindFirstChildOfClass(className)

what line is 7 on this? guessing the FireClient

It was supposed to be an fix but ignore it.

where is the error occurring at which line of code?

Anything that has [human.Parent.Name]

I told you what the problem is, human is nil, do some checks to see if it exists. Nil doesn’t have any parents, therefore, it errors.

1 Like

ok that means your Humanoid is nil, not being set or parent is not set at the time code runs like Lighting said above here are a few things that I do on touch events that may help you out

To get your Humanoid Character and Player use:

local Humanoid = hit.Parent:FindFirstChild(‘Humanoid’)
local Character = hit.Parent
local Player = game.Players:GetPlayerFromCharacter(hit.Parent) – if you are using the Character Variable you can put it here instead of hit.Parent

on your code if you setup with those variables above you could change all your human.Parent to Character
and below on your first if statement you could code it like this which will just return if there isn’t a Humanoid. Humanoid.Parent or your bag is not found

you can replace your top statement with this 1 line statement

if not Humanoid or not Humanoid.Parent or not Character:FindFirstChild(“Bag”) then return end

on the code you have I do see a miss spell on your folder Events.CilentEvent*

1 Like

I suppose you can do that, but it’s still nil if you don’t check if it’s not. It’ll happen for every collision, you can use any method to get the humanoid just check that it’s not nil.

The humanoid in this case is parented, but not every part is in the character, and parts outside of the character don’t have a humanoid, you’ll get the same error for every collision if you don’t do checks.

1 Like
local buttonPressed = false
script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild('Humanoid')
	local Character = hit.Parent
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- if you are using the Character Variable you can put it here instead of hit.Parent
	if not Humanoid or not  Humanoid.Parent or not Character:FindFirstChild("Bag") then
		script.Parent.Username.Value = Player.Name
		if not buttonPressed then	
			buttonPressed = true
				script.Parent.Sound:Play()
				game.ReplicatedStorage.PlayerSettings.Animation[Player.Name].Value = "None"
				game.ReplicatedStorage.PlayerSettings.ItemUsed[Player.Name].Value = false
				game.Players[Player.Name].PlayerGui.Menus.Options.RemoveSB.Visible = false
				workspace[Player.Name].StinkyBag:Destroy()
				local OutputText = game.ReplicatedStorage.OutputText:Clone()	
				OutputText.TextColor3 = Color3.fromRGB(0, 255, 0)
				OutputText.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
				OutputText.Text = "Successfully took out used diaper bag."
				OutputText.LocalScript.Disabled = false
				OutputText.Parent = game.Players[Player.Name].PlayerGui:FindFirstChild("Menus").Outputs
				local OutputText2 = game.ReplicatedStorage.OutputText:Clone()	
				OutputText2.TextColor3 = Color3.fromRGB(74, 225, 109)
				OutputText2.TextStrokeColor3 = Color3.fromRGB(41, 126, 60)
				OutputText2.Text = "+30 Littlebucks"
				OutputText2.LocalScript.Disabled = false
				OutputText2.Parent = game.Players[Player.Name].PlayerGui:FindFirstChild("Menus").Outputs
				game.Players[Player.Name].leaderstats.Littlebucks.Value = game.Players[Player.Name].leaderstats.Littlebucks.Value + 30
				script.Parent.Username.Value = ""
				wait(4.75)
				buttonPressed = false
		end
	end
end)

It’s giving me Nil values

You made the code only proceed if there is no Humanoid. Therefore, it will do nothing when a real character touches it, and will definitely error when a non-character touches it.
Change it to:

if Humanoid and Character:FindFirstChild("Bag") then

This will make it only run when the hit part is a part of a character, and the character also has a Bag in it.
The not Humanoid.Parent is redundant, because you know that Humanoid.Parent must exist; it is hit.Parent, which must exist.

1 Like