Move script not working

I am currently making a Raise a Floppa-like game, but my Move script for the little guy isn’t working. I’ve tried many variations of it, but with no luck. Here it is:

local NPC = script.Parent
local humanoid = NPC.Humanoid
local HumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")
local VectorForce = Instance.new("VectorForce", HumanoidRootPart)

humanoid:GetPropertyChangedSignal("WalkToPoint"):Connect(function()
	if HumanoidRootPart.Velocity.Magnitude > 30 then
		VectorForce.Force = (humanoid.WalkToPoint - HumanoidRootPart.Position).Unit * 10000
	else
		VectorForce.Force = (humanoid.WalkToPoint - HumanoidRootPart.Position).Unit * math.random(10000, 12000)
	end
	if humanoid.WalkToPoint == nil then
		VectorForce.Force = Vector3.new(0, 0, 0)
	end
end)

If you think you know what went wrong, please let me know. Thank you in advance and have a wonderful day! :slight_smile:

3 Likes

Is your model anchored? If the model or primary part of the model is anchored then the model won’t move.

1 Like

The HRP was anchored, but I unanchored it and it still won’t move.

1 Like

I really don’t know much about move scripts, so I probably did something that broke everything lol.

2 Likes

It looks like your Move script is dependent on a property named “WalkToPoint” which is not a built-in property of the Humanoid object in Roblox.

I’m assuming that you have created a custom property called “WalkToPoint” on the humanoid object in order to get the NPC to move to a specific point. However, you’ll need to make sure that you are setting the “WalkToPoint” property correctly, as well as removing it when the NPC has reached its destination.

Assuming that the “WalkToPoint” property is correctly set, the issue with your Move script may be that the “GetPropertyChangedSignal” method is not being triggered when the “WalkToPoint” property is changed. Instead of using the “GetPropertyChangedSignal” method, you can use a while loop to continually check if the “WalkToPoint” property is set, and then move the NPC accordingly. Here’s an example of what that could look like:

local NPC = script.Parent
local humanoid = NPC.Humanoid
local HumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")
local VectorForce = Instance.new("VectorForce", HumanoidRootPart)

while humanoid.WalkToPoint do
local direction = humanoid.WalkToPoint - HumanoidRootPart.Position
if direction.Magnitude > 1 then
VectorForce.Force = direction.Unit * math.random(10000, 12000)
else
VectorForce.Force = Vector3.new(0, 0, 0)
humanoid.WalkToPoint = nil
end
wait()
end

This while loop will run as long as the “WalkToPoint” property is set, and will continually move the NPC towards the specified point until it reaches its destination. Once the NPC reaches its destination, the “WalkToPoint” property is set to nil, which will cause the while loop to exit.

Keep in mind that this is just one possible implementation of a Move script, and there are many other ways to achieve NPC movement in Roblox. I hope this helps!

1 Like

I ran the code, but it’s still not moving for some reason, and I really don’t know why.

1 Like

Try this code, I used TweenService this time.

local NPC = script.Parent
local humanoid = NPC.Humanoid
local HumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")

-- Set the destination point for the NPC to walk to
local destinationPoint = Vector3.new(10, 3, 5)

-- Calculate the distance between the NPC and the destination point
local distance = (destinationPoint - HumanoidRootPart.Position).Magnitude

-- Set up the TweenInfo object for the TweenService
local tweenInfo = TweenInfo.new(distance / 10, Enum.EasingStyle.Linear)

-- Create the Tween object to move the NPC
local tween = game:GetService("TweenService"):Create(HumanoidRootPart, tweenInfo, {Position = destinationPoint})

-- Start the Tween
tween:Play()
1 Like

I’m sorry, but it’s still not working. I’m so confused.

1 Like

Did you set everything? If so and it still doesn’t work, let me know.

1 Like

The NPC itself isn’t anchored and neither is the HRP. I copy and pasted both scripts into a regular sever script parented to the NPC, but nothing worked.

1 Like

Maybe this will do? Do note that this script assumes that the NPC’s parts have “CanCollide” set to false by default. If they are already set to true, you may need to modify the code to disable collision detection instead.

local NPC = script.Parent
local humanoid = NPC.Humanoid
local HumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")

-- Set the destination point for the NPC to walk to
local destinationPoint = Vector3.new(10, 3, 5)

-- Calculate the distance between the NPC and the destination point
local distance = (destinationPoint - HumanoidRootPart.Position).Magnitude

-- Set up the TweenInfo object for the TweenService
local tweenInfo = TweenInfo.new(distance / 10, Enum.EasingStyle.Linear)

-- Create the Tween object to move the NPC
local tween = game:GetService("TweenService"):Create(HumanoidRootPart, tweenInfo, {Position = destinationPoint})

-- Set up collision detection
local function onPartCollision(otherPart)
    if otherPart.CanCollide then
        -- Stop the Tween if the NPC collides with another collidable part
        tween:Cancel()
    end
end

-- Enable collision detection on all parts of the NPC
for _, part in ipairs(NPC:GetDescendants()) do
    if part:IsA("BasePart") then
        part.CanCollide = true
        part.Touched:Connect(onPartCollision)
    end
end

-- Start the Tween
tween:Play()
1 Like

The NPC itself and the HRP’s CanCollide property has to be set to false, right?

1 Like

Yes, if you want the NPC and its HumanoidRootPart to pass through other objects without colliding with them, you should set their CanCollide property to false.

1 Like

I don’t want the NPC walking through any parts or models on the map, but I saw that in your last code sample, you said the CanCollide property has to be set to false. It is on both the NPC and the HRP, but I don’t want it to walk through things. Is there a way to adjust this and make the collision script work without erroring?

1 Like

Yes, if you want the NPC to collide with other parts and models in the game world, you should set their CanCollide property to true. However, this may cause the NPC to get stuck on obstacles or to move around them in an unnatural way.

One way to handle collisions while still allowing the NPC to move smoothly is to use a Collision Group. A Collision Group is a collection of parts that can collide with each other, but not with parts in other groups.

1 Like

That makes sense. I’m kind of a beginner to a lot of scripting, and I really don’t know a lot about CollisionGroups, though.

1 Like

This link will significantly improve your understanding of collision groups. I’ll try to write a code that can assist you in the interim. x

Collision Filtering

1 Like

Okay then, thank you for your help!

1 Like
local NPC = script.Parent
local humanoid = NPC.Humanoid
local HumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")
local destinationPoint = Vector3.new(10, 3, 5)

local pathfindingService = game:GetService("PathfindingService")
local path = pathfindingService:CreatePath()

-- Set the CollisionGroupId property on all parts of the NPC to a unique Id
for _, part in ipairs(NPC:GetDescendants()) do
    if part:IsA("BasePart") then
        part.CanCollide = true
        part.CollisionGroupId = NPC:GetAttribute("CollisionGroupId") or 0
    end
end

-- Function to move the NPC to the destination point
local function moveTo(destination)
    local success, pathResult = pcall(function()
        return path:ComputeAsync(HumanoidRootPart.Position, destination)
    end)
    if success and pathResult then
        pathResult:DrawDebugVisualization()
        local waypoints = pathResult:GetWaypoints()
        for _, waypoint in ipairs(waypoints) do
            local distance = (waypoint.Position - HumanoidRootPart.Position).Magnitude
            local time = distance / humanoid.WalkSpeed
            local tween = game:GetService("TweenService"):Create(HumanoidRootPart, TweenInfo.new(time), {Position = waypoint.Position})
            tween:Play()
            tween.Completed:Wait()
        end
    end
end

-- Move the NPC to the destination point
moveTo(destinationPoint)
1 Like

I actually solved this a while back, but I forgot I made this post lol.

The solution was to just rewrite the entire script using PathfindingService, and for some reason it never worked until recently, but hey! At least it’s functioning now. Also, I figured out why my original code never worked: I never changed the Humanoid’s WalkToPoint property. :man_facepalming:

Thank you to everyone who helped me with this and have a wonderful day! :slight_smile:

1 Like