Help with Touched Event

Hello, devs! I am trying to make a part which when touched, sends a message with a Discord bot. The webhook works but it says that I am attempting to index nil with the value. I tried using WaitForChild() but it didn’t work either. Here is the script.

local httpservice = game:GetService("HttpService")

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	local randomNum = math.random(1, 3)
	
	local check = player.AnnounceCheck
	
	if check.Value == 0 then
	    if randomNum == 1 then
	         local Data = {
	          ["content"] = player.Name.." has beat Level 1! Congrats!"
		     }
		
		     Data = httpservice:JSONEncode(Data)

			httpservice:PostAsync("Private link.", Data)
			
			check.Value = 1
			
			print("Sent!")
			
			return player
		end
		if randomNum == 2 then
			local Data = {
				["content"] = player.Name.." has beat Level 1! Yay!"
			}

			Data = httpservice:JSONEncode(Data)

			httpservice:PostAsync("Private link.", Data)

			check.Value = 1
			
			print("Sent!")
			
			return player
		end
		if randomNum == 3 then
			local Data = {
				["content"] = player.Name.." has beat Level 1! Let's party!"
			}

			Data = httpservice:JSONEncode(Data)

			httpservice:PostAsync("Private link.", Data)

			check.Value = 1
			
			print("Sent!")
			
			return player
		end
    end
end)

Could you provide an actual copy of what it says in log?

Sorry I didn’t respond fast. I was sleeping. What do you mean by in the log?

What do you see in the output window? Are there any errors?

In my output, it would say that I’m attempting to index nil with a value. I was confused cause there were no errors a few days ago and my character wasn’t touching the part.

Since you’re using a .touched event it will activate if anything touches it even if it isn’t the player. There’s no if statement checking to see if what’s being hit is actually the player or not and it could just be a part touching it which would most likely be what’s giving you your issue.

1 Like

Ok, I will try that! Thank you!

Glad to be of help. Lemme know if there’s an issue.

It works. Probably the reason why it thought to index nil is because I have it with the same position as another part. Thanks for your help!