I have a script where a rig moves around waypoints. I want to make the rig check if a player is running at a specific speed or higher. The problem is, the rig never checks the player's speed. I even made it stop for a millisecond and then check, but that still doesn’t work. This has been an issue for about a week now, and I don’t know how to fix it. I’d appreciate your help. Thanks!
#Script just doesn't want to work no matter what i do
125 messages · Page 1 of 1 (latest)
local rig = script.Parent
local playerDetentionPart = workspace.PlayerD
local principalDetentionPart = workspace.PrincipalD
local speedThreshold = 20
local normalSpeed = 28
local chaseSpeed = 32
local maxDetentionTime = 90
local initialDetentionTime = 15
local detentionTime = initialDetentionTime
local humanoid = rig:FindFirstChild("Humanoid")
local rootPart = rig:FindFirstChild("HumanoidRootPart")
local pathfindingService = game:GetService("PathfindingService")
local isChasing = false
local connections = {
["1"] = {"22"},
["2"] = {"22"},
["3"] = {"4", "6", "22"},
["4"] = {"3", "5"},
["5"] = {"4"},
["6"] = {"3", "12", "7"},
["7"] = {"6", "20"},
["20"] = {"7", "21", "8"},
["21"] = {"20"},
["8"] = {"21", "9"},
["9"] = {"8", "10", "Detention"},
["10"] = {"9", "11"},
["11"] = {"10", "12"},
["12"] = {"11", "13", "6"},
["13"] = {"14", "12", "16"},
["14"] = {"13", "15"},
["15"] = {"14"},
["16"] = {"13", "17"},
["17"] = {"16", "18"},
["18"] = {"17", "19"},
["19"] = {"9", "18", "Detention"},
["22"] = {"2", "3", "1"},
["Detention"] = {"9", "19"}
}
local function moveToWaypoint(waypoint)
if not humanoid or not rootPart then
print("Humanoid or rootPart missing, can't move")
return
end
local path = pathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 7,
AgentMaxSlope = 45
})
path:ComputeAsync(rootPart.Position, waypoint.Position)
if path.Status == Enum.PathStatus.Success then
print("Pathfinding successful, moving to waypoint")
for _, waypoint in ipairs(path:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
print("Pathfinding failed, teleporting to waypoint")
rootPart.CFrame = CFrame.new(waypoint.Position + Vector3.new(0, 5, 0))
end
end
local function chasePlayer(playerRootPart, player)
if isChasing then
print("Already chasing, aborting new chase")
return
end
isChasing = true
humanoid.WalkSpeed = chaseSpeed
print("Chasing player:", player.Name)
while (playerRootPart.Position - rootPart.Position).Magnitude > 5 do
local path = pathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 7,
AgentMaxSlope = 45
})
path:ComputeAsync(rootPart.Position, playerRootPart.Position)
if path.Status == Enum.PathStatus.Success then
print("Following player path")
for _, waypoint in ipairs(path:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
if (playerRootPart.Position - rootPart.Position).Magnitude <= 5 then
print("Close to player, breaking chase loop")
break
end
end
else
print("Pathfinding failed during chase")
break
end
end
print("Chase ended, resetting speed")
humanoid.WalkSpeed = normalSpeed
isChasing = false
end
local function patrol()
local waypointsFolder = workspace:FindFirstChild("FacultyWaypoints")
if not waypointsFolder then
print("Waypoints folder missing, cannot patrol")
return
end
local startWaypoint = waypointsFolder:FindFirstChild("9")
if not startWaypoint then
print("Start waypoint missing, cannot patrol")
return
end
print("Patrolling started from waypoint:", startWaypoint.Name)
while true do
if isChasing then
task.wait(0.1)
else
local nextWaypoints = connections[startWaypoint.Name]
if nextWaypoints then
local nextWaypoint = waypointsFolder:FindFirstChild(nextWaypoints[math.random(#nextWaypoints)])
if nextWaypoint then
print("Moving to waypoint:", nextWaypoint.Name)
moveToWaypoint(nextWaypoint)
startWaypoint = nextWaypoint
else
print("Next waypoint missing, skipping")
end
else
print("No connections for waypoint:", startWaypoint.Name)
end
end
end
end
local trackedHumanoids = {}
task.spawn(function()
while true do
task.wait(0.1)
for _, player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
if character then
local playerHumanoid = character:FindFirstChild("Humanoid")
local playerRootPart = character:FindFirstChild("HumanoidRootPart")
if playerHumanoid and playerRootPart then
if not trackedHumanoids[playerHumanoid] then
print("Tracking player:", player.Name)
trackedHumanoids[playerHumanoid] = true
playerHumanoid.Changed:Connect(function(property)
if property == "WalkSpeed" then
print("Detected player speed:", playerHumanoid.WalkSpeed)
task.wait(0.1)
if playerHumanoid.WalkSpeed >= speedThreshold then
print(player.Name .. " caught running, chasing now")
chasePlayer(playerRootPart, player)
else
print("No player running detected")
end
end
end)
end
else
print("Humanoid or rootPart missing for player:", player.Name)
end
end
end
end
end)
task.spawn(patrol)
bro nobody is going to read all that 🙏
ok
u dont have to
if u dont wanna help ur choice
i don't think u have to help so
idrc if u dont
bro got mad because i told the truth 🙏
why do you want to check if a player is running at a specific speed or higher?
so the rig follows him and sents him to detention its like baldis basics yk
which is basically rule breaking
i dont know what baldis basics is
game
put it in your own words
teacher thats bald u have to collect notebooks and there are characters trying to prevent u
one of them is called principal
and he gets you if u break a rule
one and the only one being running
why do you want to check if a player is running at a specific speed or higher?
does your game have a sprinting mechanic?
all the more reason to simplify the problem
if(sprinting and humanoid.movedirection.magnitude>0)then isrunning=true end
so i just make it like that what u send me and i should work?
the one part
or wait im confused where do i put this
don't plagiarise. make it your own
i dont know that word
i'm not going to give you copy+paste code
ok understandable
but i need to know how to fix it like do i use remote events to check between scripts or what
pathfinding is not a beginner subject
where did you get this code?
well im not really beginner
** You are now Level 4! **
also ive looked up how to use it
im like medium ig not great but not bad
its complicated to say
and i dont think its hard to make a rig go from part to part
presumably you wrote this code yourself, you understand how it works and how it is supposed to work better than anyone else because you wrote it. you asked for a simple detection for sprinting because you have sprinting system. short answer is if(sprinting and humanoid.movedirection.magnitude>0)then isrunning=true end. long answer is targetfinding which filters on that condition. that's a subject on its own like pathfinding is also a subject on its own. you do seem to have some kind of target finding already in your code with trackedHumanoids and playerHumanoid.WalkSpeed >= speedThreshold. why do you have the variable trackedHumanoids and what is that condition supposed to do?
i dont really understand
oh
u mean what trackedhumanoids is supposed to do
or something else
im not really that good in english
why did you write that code?
i made it so it knows if a player has been in detention or caught before
because if u go multiple times its 15 seconds more
or is it not how you do that
i mean i have a few months of knowledge so i may or may not have wrote something that doesnt seem to make sense without knowing it
there does not appear to be any code shown that deals with this
oh then i wrote it wrong
i was asking about this function you posted
why is trackedHumanoids in it and what is it for?
is that what chatgpt told you?
no
wait
lemme send the code to gpt and check what he says
because im really really confused
i'm just trying to understand if you wrote this code yourself, it doesn't seem like it
i did for the most part and a bit i got helped
because ive had problems before
someone told me to use that
tracked humanoids thing
i thought it was important but ig the guy lied to me
it is important
that's roughly where you would put it but it doesn't seem like it's implemented properly
oh my bad
i tried to do what that guy told me
i was confused because his english was either to good or to bad for me
npc ai can be difficult for beginners
yea ive realised that
i wanted to avoid it but i thought its gonna be easy because there are like only 2 things or systems one for walking which was hard in the beginning but easier after and then the running part was where i got stuck
with the detention or idk
pretty much
wandering, and attacking
wandering isn't too hard
attacking means you need to target players and change state
if u wanna call it attacking then yea but its not supposed to replicate attacking but yea ig
wym with change stat
e
between wandering and actively chasing a player 'attacking'
ah
consider if they had a gun you would make the attacking state completely different like finding cover and shooting guns
reloading etc
but what is the problem in the script now is it the humanoidtracked or the looking if players running
the problem is you bruh
you have this code here and don't understand how or why it is
i do understand but a guy tried helping me
spoonfeeding code is not helping in the longterm. it's short term only
and said i had to use the humanoid tracked then he gave me a script and said this should work
pick apart the code they gave you and try to understand what it is doing
same for your existing code
add prints everywhere
break it and then fix it, etc
change stuff
add more waypoints, add duplicates with different waypoints, etc

because i made like the entire map full with it
no i'm saying play with it and figure out how it works
oh ok
the docs are your friend, so is google
if you haven't read the docs page for every single function in your entire script, you're doing it rong
I have read all the docs pages for every function in your script, why haven't you? lazy
i have read them
read them again. refreshers are always good 🙂
ok