So I am making a test script just to make sure that my script works (I was going to make slender man-like movement, but first I’m making the character move to the player) but for some reason there’s no errors but nothing is happening.
I’ve tried to unanchor the character (which did not do anything), and tried to just print. I changed it though because it didn’t work. Here is my script:
local player = game.Players.LocalPlayer
local hecklebobm = script.Parent
local Torso = hecklebobm:WaitForChild("Torso")
local Humanoid = hecklebobm:WaitForChild("Humanoid")
local function findTarget()
local DISTANCE = 500
local target
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
if Torso.Position - torso.Position.magnitude < DISTANCE then
DISTANCE = (Torso.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
while wait(1) do
local torso = findTarget()
if torso then
Humanoid:MoveTo(torso.Position)
end
end
I followed this off a tutorial, so if it looks familiar that’s why. And yes, the character has a humanoid. I’ve tried 2 different plugins that insert players but they just do the same thing. No errors, doesn’t move to the player though.
Does anything appear in the output or no? You said there was no error but the line where you have no ()'s should’ve shot off an error for trying to subtract a number from a Vector3. Maybe the if statement conditions aren’t satisfied so it doesn’t return anything.
Are you sure your if statements are all getting through correctly?
(What i mean by this, is if it returns false on any of them you’re not getting any response, which i think you should be for error handling)
Are you trying to get the character to follow the player?
Also, are you doing this on a server script or a local script? It seems you are using a local script to do this.
It should be done on the server, you don’t need the player variable because the local player property is useless when used on the server, only useful when on the client
local hecklebobm = script.Parent
local Torso = hecklebobm:WaitForChild("Torso")
local Humanoid = hecklebobm:WaitForChild("Humanoid")
function findTarget()
local DISTANCE = 500
local target
for i, v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local TargetTorso = v:FindFirstChild("Torso")
if human and TargetTorso and v ~= hecklebobm then
if (Torso.Position - TargetTorso.Position).Magnitude <= DISTANCE then
DISTANCE = (Torso.Position - TargetTorso.Position).Magnitude
target = TargetTorso
return target or TargetTorso
end
end
end
return nil
end
task.spawn(function()
while wait(1) do
local torso = findTarget()
if torso then
Humanoid:MoveTo(torso.Position)
end
end
end)