In short: I am making a car programming script in my game, but I have a problem because the cars run into the NPC at the crosswalk.
So I decided to make a script that detects if the NPC is touching “Area”. If not, then print (“No NPC on the pedestrian crossing”). If so, then repeat wait () until the NPC is not on the crossing.
The problem is that:
1.My script that detects if a player is touching a part is on Scripting Helpers, and recently this platform had a big crash and I can’t get my script back.
2.I have never written any code to detect NPCs before
3.Every time I tried a new error popped up on the console.
Below is my current script for the car:
Script
local part = script.Parent
local Area = game.Workspace.TrafficParts.PedestrianCrossing
local TweenService = game:GetService("TweenService")
local TweenInfo = TweenInfo.new(
10,
Enum.EasingStyle.Quint,
Enum.EasingDirection.Out,
0,
false,
0
)
local Tween = TweenService:Create(part, TweenInfo, {Position = Vector3.new(35.062, 3.328, 48.477)})
Tween:Play()
-- I wanted to make script here
Detecting npcs is no different than detecting characters (not players), You just need to check whether they have a humanoid or not, I recommend using OverLapParams For your detection, Make sure its not all server sided since it would be better partially in the client. You could have an overlapParams and use GetPartsInPart or something similar, make sure to learn about all of that before proceeding.
Edit : Do not use Region3 use OverLapParams Instead
local run = game:GetService("RunService")
local road = script.Parent --Example road part.
local debounce = false
run.Stepped:Connect(function()
if debounce then return end
local pedestrians = {} --Pedestrians table.
local touchingParts = road:GetTouchingParts()
for _, part in ipairs(touchingParts) do
local model = part:FindFirstAncestorOfClass("Model")
if model then
local human = model:FindFirstChildOfClass("Humanoid")
if human then
if not table.find(pedestrians, model) then
table.insert(pedestrians, model) --Insert pedestrian into table.
break --Only need 1 pedestrian.
end
end
end
end
print(#pedestrians)
if #pedestrians > 0 then --Checks for pedestrians.
debounce = true --Enable debounce.
--Stop car.
task.wait(1) --Wait 1 second before performing next loop.
debounce = false --Disable debounce.
else
--No pedestrians.
--Start car.
end
end)
I’ve added comments which should hopefully explain the steps involved in this code snippet. It’s just an example of how you could go about achieving this.
Unfortunately, it didn’t work, the script only showed that the NPC was not on the road (which didn’t work anyway), and there were no messages on the output.
Did I do something wrong in this script? I understand that “Road” is the part the NPC is touching.
Script
local part = script.Parent
local Area = game.Workspace.TrafficParts.PedestrianCrossing
local TweenService = game:GetService("TweenService")
local TweenInfo = TweenInfo.new(
10,
Enum.EasingStyle.Quint,
Enum.EasingDirection.Out,
0,
false,
0
)
local Tween = TweenService:Create(part, TweenInfo, {Position = Vector3.new(35.062, 3.328, 48.477)})
Tween:Play()
-- I wanted to make script here
local run = game:GetService("RunService")
local road = Area
local debounce = false
run.Stepped:Connect(function()
if debounce then return end
local pedestrians = {} --Pedestrians table.
local touchingParts = road:GetTouchingParts()
for _, part in ipairs(touchingParts) do
local model = part:FindFirstAncestorOfClass("Model")
if model then
local human = model:FindFirstChildOfClass("Humanoid")
if human then
if not table.find(pedestrians, model) then
table.insert(pedestrians, model) --Insert pedestrian into table.
break --Only need 1 pedestrian.
end
end
end
end
print(#pedestrians)
if #pedestrians > 0 then --Checks for pedestrians.
debounce = true --Enable debounce.
--Stop car.
task.wait(1) --Wait 1 second before performing next loop.
debounce = false --Disable debounce.
print(#pedestrians)
else
print(#pedestrians)
end
end)