Advertisement
Guest User

LEGOKID SOURCE

a guest
Dec 9th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.68 KB | None | 0 0
  1. --[[
  2.     Control script for the Legokid
  3. --]]
  4.  
  5. -- Constants --
  6. local RS = game:GetService("RunService")
  7. local Players = game:GetService("Players")
  8. local ChatService = game:GetService("Chat")
  9. local RepStor = game:GetService("ReplicatedStorage")
  10. local IS = game:GetService("InsertService")
  11. local Legokid = script.Parent
  12. local Humanoid = Legokid.Humanoid
  13. local StopAction = false
  14. local Rad = math.rad
  15. local RNG = Random.new()
  16. local PlayersHooked = {}
  17.  
  18. -- Important functions --
  19. function FindInTable(Tbl,Element)
  20.     for i = 1,#Tbl do
  21.         if Tbl[i] == Element then
  22.             return true
  23.         end
  24.     end
  25.     return false
  26. end
  27.  
  28. function GetDistance(X,Y)
  29.     return (X.Position-Y.Position).Magnitude
  30. end
  31.  
  32. function Stop(Dequip)
  33.     -- Stops the Legokid's actions --
  34.     local Torso = Legokid.Torso
  35.     local LS,RiS = Torso:FindFirstChild("Left Shoulder"),Torso:FindFirstChild("Right Shoulder")
  36.     local LH,RH = Torso:FindFirstChild("Left Hip"),Torso:FindFirstChild("Right Hip")    StopAction = true
  37.            
  38.     -- This could put a small pause on any action that happens after this is called --
  39.     -- If the character has a tool equiped, put their arm down --
  40.  
  41.     spawn(function()
  42.         if Dequip and Legokid:FindFirstChildOfClass("Tool") then
  43.             for i = 1,90,6 do
  44.                 RiS.C0 = RiS.C0*CFrame.Angles(Rad(0),Rad(0),Rad(-6))
  45.                 RS.Heartbeat:Wait()
  46.             end
  47.             Humanoid:UnequipTools()
  48.         end
  49.     end)
  50.     StopAction = true
  51.     -- Set the DesiredAngles --
  52.     LS.DesiredAngle = 0
  53.     RiS.DesiredAngle = 0
  54.     LH.DesiredAngle = 0
  55.     RH.DesiredAngle = 0
  56.     wait(.1)
  57.     StopAction = false
  58. end
  59.  
  60. -- Legokid commands --
  61. local Commands = {
  62.     ["follow"] = function(...)
  63.         local Arguments = {...}
  64.        
  65.         if Arguments[1] then
  66.             -- Make the legokid stop following any preselected player --
  67.             Stop(true)
  68.            
  69.             -- Loop through all players --
  70.             for _,player in pairs(Players:GetPlayers()) do
  71.                 if player.Name:lower() == Arguments[1] then
  72.                     local Char = player.Character
  73.                     spawn(function()
  74.                         while Char.Humanoid.Health > 0 and StopAction ~= true do
  75.                             Humanoid:MoveTo(Char.HumanoidRootPart.Position)
  76.                             RS.Heartbeat:Wait()
  77.                         end
  78.                     end)
  79.                     break  
  80.                 end
  81.             end
  82.         end
  83.     end,
  84.     ["stop"] = function(...)
  85.         -- Stops the current action --
  86.         Stop(true)
  87.     end,
  88.     ["jump"] = function()
  89.         Humanoid.Jump = true
  90.     end,
  91.     ["unequip"] = function()
  92.         local RiS = Legokid.Torso["Right Shoulder"]
  93.        
  94.         -- If the character has a tool equiped --
  95.         if Legokid:FindFirstChildOfClass("Tool") then
  96.             for i = 1,90,6 do
  97.                 RiS.C0 = RiS.C0*CFrame.Angles(Rad(0),Rad(0),Rad(-6))
  98.                 RS.Heartbeat:Wait()
  99.             end
  100.         end
  101.  
  102.         -- Unequip any tools already equipped --
  103.         Humanoid:UnequipTools()
  104.     end,
  105.     ["equip"] = function(...)
  106.         local Arguments = {...}
  107.        
  108.         if Arguments[1] and RepStor:FindFirstChild(Arguments[1]) then
  109.             local Tool = RepStor:FindFirstChild(Arguments[1]):Clone()
  110.             local RiS = Legokid.Torso["Right Shoulder"]
  111.            
  112.             -- If the character has a tool equiped, move it back to the default arm rotation21 --
  113.             if Legokid:FindFirstChildOfClass("Tool") then
  114.                 -- Unequip any tools already equipped --
  115.                 Humanoid:UnequipTools()
  116.                
  117.                 -- Move the arm --
  118.                 for i = 1,90,6 do
  119.                     RiS.C0 = RiS.C0*CFrame.Angles(Rad(0),Rad(0),Rad(-6))
  120.                     RS.Heartbeat:Wait()
  121.                 end
  122.             end
  123.                        
  124.             -- Equip the given tool --
  125.             if Tool then
  126.                 for i = 1,90,6 do
  127.                     RiS.C0 = RiS.C0*CFrame.Angles(Rad(0),Rad(0),Rad(6))
  128.                     RS.Heartbeat:Wait()
  129.                 end
  130.                 Humanoid:EquipTool(Tool)
  131.                 wait(1)
  132.             end
  133.         end
  134.     end,
  135.     ["attack"] = function(...)
  136.         local Arguments = {...}
  137.         local Tool = Legokid:FindFirstChildOfClass("Tool")
  138.        
  139.         -- Did the player supply a player to follow? --
  140.         if Arguments[1] then
  141.             if Tool then
  142.                 -- Does the player exist? --
  143.                 for _,Player in pairs(Players:GetPlayers()) do
  144.                     if Player.Name:lower() == Arguments[1] then
  145.                         local Char = Player.Character
  146.                    
  147.                         -- Make the legokid stop following any preselected player --
  148.                         Stop(false)
  149.                        
  150.                         -- Follow the player till conditions met --
  151.                         spawn(function()
  152.                             while Char.Humanoid.Health > 0 and StopAction == false do
  153.                                 Humanoid:MoveTo(Char.HumanoidRootPart.Position)
  154.                                 wait()
  155.                             end
  156.                         end)
  157.                        
  158.                         -- Attack the player till conditions are met --
  159.                         spawn(function()
  160.                             while Char.Humanoid.Health > 0 and StopAction == false do
  161.                                
  162.                                 local Dist = GetDistance(Legokid.HumanoidRootPart,Char.HumanoidRootPart)
  163.                                 if Dist <= Tool.Configuration.AttackRange.Value then
  164.                                     -- Tool specific attacks --
  165.                                     if Tool.Name == "sword" then
  166.                                         local SwordMove = RNG:NextInteger(1,2)
  167.                                         -- Lunge, swing --
  168.                                         if SwordMove == 1 then
  169.                                             Tool:Activate()
  170.                                         elseif SwordMove == 2 then
  171.                                             Tool:Activate()
  172.                                             wait(.2)
  173.                                             Tool:Activate()
  174.                                         end
  175.                                     else
  176.                                         print("E")
  177.                                     end
  178.                                 end
  179.                                 wait(.3)
  180.                             end
  181.                         end)
  182.                     end
  183.                 end
  184.             end
  185.         end
  186.     end,   
  187.     ["dance"] = function()
  188.         -- Stop any actions already running --
  189.         Stop(true)
  190.        
  191.         -- Get the Motor6D's in the Torso --
  192.         local Torso = Legokid.Torso
  193.         local LS,RiS = Torso:FindFirstChild("Left Shoulder"),Torso:FindFirstChild("Right Shoulder")
  194.         local LH,RH = Torso:FindFirstChild("Left Hip"),Torso:FindFirstChild("Right Hip")
  195.        
  196.         -- Do all the Motor6D's exist? --
  197.         if LS and RiS and LH and RH then
  198.             -- Repeat UNTIL Legokid dies or is told to stop --
  199.            
  200.             -- Move DesiredAngles into starting positions --
  201.             LS.DesiredAngle = 2
  202.             RiS.DesiredAngle = 2
  203.             LH.DesiredAngle = 1
  204.             RH.DesiredAngle = 1
  205.            
  206.             -- Dance with a duration of 10 seconds unless the Character dies or is told to stop --
  207.             for i = 1,25 do
  208.                 if Humanoid.Health > 0 and StopAction == false then
  209.                     -- Boogy!! --
  210.                     print("Dancin'!")
  211.                     -- Move the arms --
  212.                     LS.DesiredAngle = 4
  213.                     RiS.DesiredAngle = 4
  214.                     LH.DesiredAngle = -1
  215.                     RH.DesiredAngle = -1
  216.                     wait(.2)
  217.                     LS.DesiredAngle = 2
  218.                     RiS.DesiredAngle = 2
  219.                     LH.DesiredAngle = 1
  220.                     RH.DesiredAngle = 1
  221.                     wait(.2)
  222.                 else
  223.                     break
  224.                 end
  225.             end
  226.            
  227.             -- Set arms to default --
  228.             LS.DesiredAngle = 0
  229.             RiS.DesiredAngle = 0
  230.             LH.DesiredAngle = 0
  231.             RH.DesiredAngle = 0
  232.         end
  233.     end,
  234.     ["chat"] = function(...) -- typing in messages will say it without spaces. maybe have them say it via a string. also add filtering
  235.         local Args = {...}
  236.        
  237.         -- If the player supplied something for the Legokid to say --
  238.         if Args[1] then
  239.             ChatService:Chat(Legokid.Head,Args[1],Enum.ChatColor.Blue)
  240.         end
  241.     end,
  242.     ["heal"] = function()
  243.         -- Heal the Legokid --
  244.         Humanoid.Health = Humanoid.MaxHealth
  245.     end,
  246.     ["speed"] = function(...)
  247.         local Args = {...}
  248.        
  249.         -- Check for a player supplied speed --
  250.         if Args[1] and tonumber(Args[1]) then
  251.             print("Speed given: " .. Args[1])
  252.             Humanoid.WalkSpeed = tonumber(Args[1])
  253.         else
  254.             -- If no speed provided, set to default --
  255.             Humanoid.WalkSpeed = 16
  256.         end
  257.     end,
  258.     ["health"] = function(...)
  259.         local Args = {...}
  260.        
  261.         -- Check for a player supplied health --
  262.         if Args[1] and tonumber(Args[1]) then
  263.             print("Health given: " .. Args[1])
  264.             Humanoid.Health = tonumber(Args[1])
  265.         else
  266.             -- If no 100 provided, set to default --
  267.             Humanoid.Health = 100
  268.         end
  269.     end,
  270.     ["clothes"] = function(...) -- allow for changing of individual pieces
  271.         local Args = {...}
  272.         -- If given clothes [1] and pants [2] arguments --
  273.         if Args[1] and Args[2] and tonumber(Args[1]) and tonumber(Args[2]) then
  274.             -- Make sure we can load in these assets --
  275.             local Success,C1,C2 = pcall(function()
  276.                 return IS:LoadAsset(Args[1]),IS:LoadAsset(Args[2])
  277.             end)
  278.            
  279.             -- If the assets successfully loaded in --
  280.             if Success then
  281.             -- Destroy any existing pants and replace them --
  282.                 local LegokidChildren = Legokid:GetDescendants()
  283.                
  284.                 for object = 1,#LegokidChildren do
  285.                     if LegokidChildren[object]:IsA("Shirt") or LegokidChildren[object]:IsA("Pants") then
  286.                         LegokidChildren[object]:Destroy()
  287.                         print("Destroyed clothing item.")
  288.                     end
  289.                 end
  290.                
  291.                 C1.Shirt.Parent = Legokid
  292.                 C2.Pants.Parent = Legokid
  293.                
  294.                 print("Replaced clothing.")
  295.             else
  296.                 warn("Could not load asset.")
  297.             end
  298.         end
  299.     end,
  300.     ["goto"] = function(...)
  301.         -- Moves the NPC to specified coordinates. --
  302.         -- Defaults Coords to Vector3.new(0,0,0)
  303.         Stop()
  304.        
  305.         local Args = {...}
  306.        
  307.         -- If provided arguments, and they're all numbers --
  308.         if Args[1] and Args[2] and Args[3] and tonumber(Args[1]) and tonumber(Args[2]) and tonumber(Args[3]) then
  309.             Humanoid:MoveTo(Vector3.new(Args[1],Args[2],Args[3]))
  310.         else
  311.             Humanoid:MoveTo(Vector3.new(0,0,0))
  312.         end
  313.     end
  314. }
  315.  
  316. function ParseMessage(msg)
  317.     --[[
  318.         This Function parses any given message for data
  319.         required by the commands. It will only return any
  320.         arguments and the message under certain conditions, these are:
  321.        
  322.         If the legokid isn't dead
  323.             If the parsed message begins with "legokid"
  324.                 If it has an actual command inside of it
  325.     --]]
  326.    
  327.     if Humanoid.Health > 0 then
  328.         local MsgIdentifier = msg:sub(1,7):lower()
  329.         local MsgRemainder = msg:sub(9,-1):lower() -- -1 indicating the ending of the str --
  330.        
  331.         -- Check if the first 7 characters in the string are "legokid" --
  332.         if MsgIdentifier == "legokid" then
  333.             -- Loop through all commands --
  334.             for command,func in pairs(Commands) do
  335.                 local CommandEmbded = MsgRemainder:sub(1,command:len())
  336.                 local FinalRemainder = ((MsgRemainder:gsub(" ","")):sub(command:len()+1,-1))
  337.                
  338.                 -- If the command is in the beginning of MsgRemainder --
  339.                 if CommandEmbded == command then
  340.                     print("Found command", command)
  341.                     print("Remainder of command:",FinalRemainder)
  342.                    
  343.                     -- Extract arguments --
  344.                     local Arguments = FinalRemainder:split(",")
  345.                     func(unpack(Arguments))
  346.                 else
  347.                     print("Command wasn't found on this key")
  348.                 end
  349.             end
  350.         else
  351.             print("'legokid' not in beginning of command")
  352.         end
  353.     end
  354. end
  355.  
  356. while wait(.1) do
  357.     for index,plr in pairs(Players:GetPlayers()) do
  358.         -- Check if player isn't in our list of hooked players --
  359.         if FindInTable(PlayersHooked,plr.Name) == false then
  360.             -- Add them to table of hooked players --
  361.             table.insert(PlayersHooked,plr.Name)
  362.            
  363.             -- If the player isn't hooked, hook them --
  364.             plr.Chatted:Connect(function(msg)
  365.                 print("Chatted")
  366.                 ParseMessage(msg:lower())
  367.             end)
  368.            
  369.         -- If they are in it, check if they're still in the game --
  370.         else
  371.             -- If they're not still in the game, remove them from the table --
  372.             -- So if they rejoin, it'll still re-hook them --
  373.             for HookedIndex,HookedPlayer in pairs(PlayersHooked) do
  374.                 if not Players:FindFirstChild(HookedPlayer) then
  375.                     table.remove(PlayersHooked,index)
  376.                 end
  377.             end
  378.         end
  379.     end
  380. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement