Advertisement
silver_est

snake

Aug 4th, 2015 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.82 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  2. -- Snake script
  3. -- made by masterofteamwork2
  4. -- A part of project PIRN©
  5. -- This script is free to use for anyone.
  6. -- I have tried to add as much comments as possible so you can understand how it works.
  7. workspace = game.Workspace
  8. base = game.Workspace.Base
  9. snake = Instance.new("Model", workspace)
  10. snake.Name = "Snake"
  11. parts = {}
  12. speed = 1
  13. lastloc = Vector3.new(0,base.Position.Y + base.Size.Y/2 + speed/2,0)
  14. lastpart = nil
  15. length = 20
  16. main = nil
  17. speed2 = 0.2
  18. headcolor = BrickColor.Yellow()
  19. tailcolor1 = BrickColor.Black()
  20. tailcolor2 = BrickColor.White()
  21. cancollide = true
  22. color = true
  23. function createmp()
  24.     local mainp = Instance.new("Part",snake)
  25.     mainp.Name = "head"
  26.     mainp.FormFactor = "Custom"
  27.     mainp.Size = Vector3.new(speed,speed,speed)
  28.     mainp.Anchored = true
  29.     mainp.BrickColor = headcolor
  30.     mainp.CanCollide = cancollide
  31.     mainp.CFrame = CFrame.new(0,base.Position.Y + base.Size.Y/2 + mainp.Size.Y/2,0)
  32.     main = mainp
  33. end
  34. function createp(pos,name)
  35.     --print("Creating a part named "..name)
  36.     local p = Instance.new("Part",snake)
  37.     p.Name = tostring(name)
  38.     p.FormFactor = "Custom"
  39.     p.Size = Vector3.new(speed,speed,speed)
  40.     p.Anchored = true
  41.     p.CanCollide = cancollide
  42.     if color == true then
  43.         color = false
  44.         p.BrickColor = tailcolor1
  45.     else
  46.         color = true
  47.         p.BrickColor = tailcolor2
  48.     end
  49.     p.CFrame = CFrame.new(pos.X,pos.Y,pos.Z)
  50.     --game:GetService("Debris"):AddItem(p, 3)
  51. end
  52. function cont()
  53.     createmp()
  54.     count1 = 0
  55.     for i = 1, 100 do
  56.         dir = math.random(1,4)
  57.         for t = 1,5 do
  58.             count1 = count1 + 1
  59.             wait(speed2)
  60.             done = move(dir,count1)
  61.             if done == true then
  62.             else
  63.             dir = math.random(1,4)
  64.             end
  65.         end
  66.     end
  67. end
  68. function getlastpart(co)
  69.     a = snake:GetChildren()
  70.     smallest =  co
  71.     --print("Items in snake: "..#a)
  72.     for i = 1,#a do
  73.         c = a[i]
  74.         --print(i.." item is "..c.Name)
  75.         if c.Name ~= "head" then
  76.             --print("not head")
  77.             if tonumber(c.Name) < smallest then
  78.                 smallest = tonumber(c.Name)
  79.             end
  80.         end
  81.     end
  82.     lastpart = snake:FindFirstChild(tostring(smallest))
  83. end
  84. function move(dir,i)-- moves in the direction given
  85.     --print(dir)
  86.     getlastpart(i)
  87.     if dir == 1 then
  88.        
  89.         if lastloc ~= Vector3.new(main.Position.X-speed,main.Position.Y,main.Position.Z) then
  90.             lastloc = main.Position
  91.             main.CFrame = CFrame.new(main.CFrame.X-speed,main.CFrame.Y,main.CFrame.Z)
  92.             if lastpart ~= nil and i > length then
  93.                 lastpart:Remove()
  94.             end
  95.             createp(lastloc,i)
  96.             return true
  97.         else
  98.             return false
  99.         end
  100.     elseif dir == 2 then
  101.         if lastloc ~= Vector3.new(main.Position.X+speed,main.Position.Y,main.Position.Z) then
  102.             lastloc = main.Position
  103.             main.CFrame = CFrame.new(main.CFrame.X+speed,main.CFrame.Y,main.CFrame.Z)
  104.             if lastpart ~= nil and i > length then
  105.                 lastpart:Remove()
  106.             end
  107.             createp(lastloc,i)
  108.             return true
  109.         else
  110.             return false
  111.         end
  112.     elseif dir == 3 then
  113.         if lastloc ~= Vector3.new(main.Position.X,main.Position.Y,main.Position.Z-speed) then
  114.             lastloc = main.Position
  115.             main.CFrame = CFrame.new(main.CFrame.X,main.CFrame.Y,main.CFrame.Z-speed)
  116.             if lastpart ~= nil and i > length then
  117.                 lastpart:Remove()
  118.             end
  119.             createp(lastloc,i)
  120.             return true
  121.         else
  122.             return false
  123.         end
  124.     elseif dir == 4 then
  125.         if lastloc ~= Vector3.new(main.Position.X,main.Position.Y,main.Position.Z-speed) then
  126.             lastloc = main.Position
  127.             main.CFrame = CFrame.new(main.CFrame.X,main.CFrame.Y,main.CFrame.Z-speed)
  128.             if lastpart ~= nil and i > length then
  129.                 lastpart:Remove()
  130.             end
  131.             createp(lastloc,i)
  132.             return true
  133.         else
  134.             return false
  135.         end
  136.     else
  137.     debug("Direction given is not correct!")
  138.     end
  139. end
  140. function debug(msg) -- Shows a message
  141.     local m = Instance.new("Hint",workspace)
  142.     m.Text = msg
  143.     m.Name = "Notification"
  144.     game:GetService("Debris"):AddItem(m, 2)
  145. end
  146. cont()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement