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!
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!
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()
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.
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()
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.
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?
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.
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)
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.
Thank you to everyone who helped me with this and have a wonderful day!