#Script just doesn't want to work no matter what i do

125 messages · Page 1 of 1 (latest)

vague solar
#

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!

#
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)
winged flame
#

bro nobody is going to read all that 🙏

vague solar
#

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

winged flame
#

bro got mad because i told the truth 🙏

shy cargo
vague solar
#

which is basically rule breaking

shy cargo
#

i dont know what baldis basics is

vague solar
#

game

shy cargo
#

put it in your own words

vague solar
#

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

shy cargo
vague solar
#

so if hes running he gets sent to detention

#

i have a running system

shy cargo
vague solar
#

yep

#

speed is 28 i wanted to make 20 so it can be detected better

shy cargo
#

why not just check if sprinting and moving

#

can ignore actual move speed

vague solar
#

idk how to do that

#

do i use remote event?

shy cargo
#

all the more reason to simplify the problem

#

if(sprinting and humanoid.movedirection.magnitude>0)then isrunning=true end

vague solar
#

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

shy cargo
vague solar
#

i dont know that word

shy cargo
vague solar
#

ok understandable

#

but i need to know how to fix it like do i use remote events to check between scripts or what

shy cargo
#

where did you get this code?

vague solar
#

well im not really beginner

plucky jewelBOT
#

studio** You are now Level 4! **studio

vague solar
#

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

shy cargo
# vague solar but i need to know how to fix it like do i use remote events to check between sc...

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?

vague solar
#

i dont really understand

#

oh

#

u mean what trackedhumanoids is supposed to do

#

or something else

#

im not really that good in english

shy cargo
vague solar
#

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

shy cargo
vague solar
#

oh then i wrote it wrong

shy cargo
#

why is trackedHumanoids in it and what is it for?

vague solar
#

i meant i told u

#

so it knows when a player has been caught before

shy cargo
#

is that what chatgpt told you?

vague solar
#

no

#

wait

#

lemme send the code to gpt and check what he says

#

because im really really confused

shy cargo
vague solar
#

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

shy cargo
#

it is important

#

that's roughly where you would put it but it doesn't seem like it's implemented properly

vague solar
#

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

shy cargo
#

npc ai can be difficult for beginners

vague solar
#

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

shy cargo
#

pretty much

#

wandering, and attacking

#

wandering isn't too hard

#

attacking means you need to target players and change state

vague solar
#

if u wanna call it attacking then yea but its not supposed to replicate attacking but yea ig

#

wym with change stat

#

e

shy cargo
#

between wandering and actively chasing a player 'attacking'

vague solar
#

ah

shy cargo
#

consider if they had a gun you would make the attacking state completely different like finding cover and shooting guns

#

reloading etc

vague solar
#

but what is the problem in the script now is it the humanoidtracked or the looking if players running

shy cargo
#

you have this code here and don't understand how or why it is

vague solar
#

i do understand but a guy tried helping me

shy cargo
#

spoonfeeding code is not helping in the longterm. it's short term only

vague solar
#

and said i had to use the humanoid tracked then he gave me a script and said this should work

shy cargo
#

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

vague solar
#

ok ill try fixing the humanoids tracked

#

do i need more waypoints

cold shadow
vague solar
#

because i made like the entire map full with it

shy cargo
#

no i'm saying play with it and figure out how it works

vague solar
#

oh ok

shy cargo
#

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

vague solar
#

i have read them

shy cargo
#

read them again. refreshers are always good 🙂

vague solar
#

ok

shy cargo
#

you need to put in effort to change things up and make it your own

#

copy+pasting wont teach you anything, but pulling it apart and making it do whatever is on your mind, will.