What's wrong with my model?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Fix the model pathfinding and walking

  2. What is the issue? robloxapp-20210814-1114457.wmv (3.1 MB)

  3. What solutions have you tried so far? Fixing the rig(but still)

The model:

The AI Script:

local humanoid = teddy.Humanoid

for i, v in pairs(teddy:GetChildren()) do
	if v:IsA("Part") or v:IsA("MeshPart") then
		v.Anchored = false
	end
end

local collection = game:GetService("CollectionService")

teddy.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
	local raycastParams = RaycastParams.new()
	local origin = teddy.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 40
	--local ray = Ray.new(origin, direction)
	raycastParams.FilterDescendantsInstances = {workspace.Town.SecretPlace2.forraycast}
	raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
	local hit, pos = workspace:Raycast(origin, direction )
	if hit then
		local hitpart = hit.Instance	
		if hitpart:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end


local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
		
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
		
	return nearestTarget
end

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")
		
	local pathParams = {
		["AgentHeight"] = 11.9,
		["AgentRadius"] = 8,
		["AgentCanJump"] = false
	}
		
	local path = PathfindingService:CreatePath(pathParams)
		
	path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)
		
	return path
end

local function attack(target)
		
	local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 10 then
		
		humanoid:MoveTo(target.HumanoidRootPart.Position)
		humanoid.MoveToFinished:Wait(2)
	elseif collection:HasTag(target, "Hidden") then
		patrol()
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		target.Humanoid.Health = target.Humanoid.Health - 100
	end
end

local function walkTo(destination)
		
	local path = getPath(destination)
		
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				--print("TARGET FOUND", target.Name)
				attack(target)
				break
			else
				--print("Moving to ", waypoint.Position)
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
	end
		
end

function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end
	
while wait(0.25) do
	patrol()
end

Yes, it’s from youtube…

Hope you can help me! Thanks!!

The main thing is that you have to understand how your script and model work before you can use it properly. Therefore, copying a script from tutorials is a bad idea. You should treat the tutorial code as an example for learning and write your own code for you to use.

2 Likes

Yea yea, it’s working now i understand the script and modify the model… Thanks!

2 Likes