Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help With A Simple Touched Function?

function ot()
	game.Workspace.Scootabot.TouchingWhat.Value = 1
end

script.Parent.Touched:connect(ot)

I have this function that sets TouchingWhat.Value to 1 when it's touched. The problem is, I don't want it to be able to be set off by a player. I want it so that only the AI walking towards the brick's part can activate the function. I already tried

local bot = script.Parent:FindFirstChild("Model")
if bot.Name = "Scootabot" then

but it came up with the conclusion that Scootakip (The NPC Model touching the block) is nil.

Can someone help me fix this incredibly annoying issue.

1 comment

0

Well, my script was correct, but if you don't like it ,that's fine.

1 answer

Touched is an event that calls bound function with an argument which holds the object that touched the Part that has the event connected.

What you have to do is check if that object is a part of your NPC. In order to do that, there are many methods.

One of the simplest ones is to check the name of Model containing that part and see if it matches your bot's name. You seem to have tried that, so I will also use that method.

function ot(hit) -- hit is a variable that Touched event passes to the function
	-- We check if part exists (just to make sure) and if it contains a Humanoid, since most NPCs do
	if hit and hit.Parent:FindFirstChild('Humanoid') then
		-- We check if the name matches
		if hit.Parent.Name == "Scootabot" then
			game.Workspace.Scootabot.TouchingWhat.Value = 1
			-- do whatever else you want
		end
	end
end

Hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.