Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

Help With A Simple Touched Function?

Asked by
Scootakip 299 Moderation Voter
8 years ago
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.

0
Well, my script was correct, but if you don't like it ,that's fine. Shawnyg 4330 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

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.

Ad

Answer this question