I am making a game similar to Davidii's Survival 404, I used his game as a base and have made a completely new map, new items, buildings, ect. One of those things I added was a horse AI that moves using something called the "NodeModel" which are basically nodes that the AI animals move to randomly using this script:
wait(3)
local ME = script.Parent
local TORSO = ME.Torso
local STATS = ME.ANIMAL
STATS.HEALTH.Value = STATS.MAXHEALTH.Value
local REGEN = ME:clone()
local NODE_RANGE = 20
local STATE = "WANDER"
function get_random_from_table(tab)
return tab[math.random(1, #tab)]
end
function get_drop_name()
local drop = STATS:findFirstChild("DROP")
return drop.Value
end
function get_nearby_node(torso, range)
local nodes = game.Workspace.NodeStorage:GetChildren()
local nearby_nodes = {}
if range == nil then
range = NODE_RANGE
end
for index, child in pairs(nodes) do
local distance = (torso.Position - child.Value).magnitude
if distance < range then
table.insert(nearby_nodes, child)
end
end
local chosen_node = nil
if #nearby_nodes < 1 then
chosen_node = get_nearest_node(TORSO)
else
chosen_node = get_random_from_table(nearby_nodes)
end
return chosen_node.Value
end
function get_nearest_node(torso)
local nodes = game.Workspace.NodeStorage:GetChildren()
local best_node = nodes[1]
local best_distance = (torso.Position - best_node.Value).magnitude
for index, child in pairs(nodes) do
local distance = (torso.Position - child.Value).magnitude
if distance < best_distance then
best_distance = distance
best_node = child
end
end
return best_node
end
function do_move_animal(torso, position, speed)
position = Vector3.new(position.x, torso.Position.y, position.z)
local bv = torso.BodyVelocity
local bg = torso.BodyGyro
local vector = (position - torso.Position)
local unit = vector.unit
local distance = vector.magnitude
local time = distance / speed
local direction = torso.Position + unit
bg.cframe = CFrame.new(torso.Position, direction)
bv.velocity = unit * speed
wait(time)
bv.velocity = Vector3.new(0, 0, 0)
end
function get_nearby_player()
local players = game.Players:GetChildren()
local best_distance = 25
local best_player = nil
for index, child in pairs(players) do
local char = child.Character
if char ~= nil then
local torso = char.Torso
local distance = (TORSO.Position - torso.Position).magnitude
if distance < best_distance then
best_distance = distance
best_player = char
end
end
end
return best_distance, best_player
end
function get_dead()
local health = STATS.HEALTH.Value
local y_pos = TORSO.Position.y
return (health <= 0 or y_pos < 20)
end
function do_die()
STATE = "DEAD"
local torso = TORSO:GetChildren()
local drop = STATS.DROP:GetChildren()
--change name of torso
TORSO.Name = get_drop_name()
--remove all from torso
for index, child in pairs(torso) do
child:remove()
end
--give drop stuff in Torso
for index, child in pairs(drop) do
if child.className == "Script" then
local new_script = child:clone()
new_script.Disabled = false
new_script.Parent = TORSO
child:remove()
else
child.Parent = TORSO
end
end
--put torso outside of char, make it forageable
TORSO.Parent = game.Workspace
TORSO.Locked = false
--"kill" me!
ME:BreakJoints()
wait(5)
--regenerate
REGEN.Parent = game.Workspace
REGEN:MakeJoints()
--remove
ME:remove()
end
function do_loop()
if STATE == "WANDER" then
--acquire a random nearby node
local node = get_nearby_node(TORSO)
--move to it
do_move_animal(TORSO, node, STATS.SPEED.Value)
--check to see if I'm in danger
local distance, player = get_nearby_player()
if distance < 25 then
STATE = "RUNNING"
end
--pause
wait(math.random(0, 2))
elseif STATE == "RUNNING" then
--acquire nearby node
local node = get_nearby_node(TORSO)
--move to it - frantically
do_move_animal(TORSO, node, STATS.SPEED.Value * 2)
--check to see if I'm no longer in danger, then wander
local distance, player = get_nearby_player()
if distance >= 25 then
STATE = "WANDER"
end
--don't pause!
wait()
end
end
--secondary health loop, which checks to see if the animal has died
delay(0, function()
while true do
if get_dead() then
do_die()
end
wait(1)
end
end)
--main loop, runs the whole show
while true do
do_loop()
wait()
end
(^This script was included to help you understand how the animal moves, this is NOT the script that I plan to use to do what I am trying to do.)
The script uses values inside the model (the AI animal) to make it animated and I think rocket propulsion values are what they are called to keep its body in the right place when moving. (Like BodyVelocity and BodyGyro). That being said, I am trying to make it so that when a brick is placed within a certain range of the AI, the values and everything else other than the actual bricks that make up the horse are removed, and replace them with a different script and some rocket propulsion values that make the horse basically a drive able vehicle (sorry if I explained that badly) I am a fairly new scripter, so I understand scripting, but I don't really know many functions. I'm asking on here to see if someone could not write the script for me, but maybe show me some of the functions I would need to do all of this. If you need me to clarify anything just ask, and thank you so much for any help, because frankly, i'm lost. :)
