Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- -- Snake script
- -- made by masterofteamwork2
- -- A part of project PIRN©
- -- This script is free to use for anyone.
- -- I have tried to add as much comments as possible so you can understand how it works.
- workspace = game.Workspace
- base = game.Workspace.Base
- snake = Instance.new("Model", workspace)
- snake.Name = "Snake"
- parts = {}
- speed = 1
- lastloc = Vector3.new(0,base.Position.Y + base.Size.Y/2 + speed/2,0)
- lastpart = nil
- length = 20
- main = nil
- speed2 = 0.2
- headcolor = BrickColor.Yellow()
- tailcolor1 = BrickColor.Black()
- tailcolor2 = BrickColor.White()
- cancollide = true
- color = true
- function createmp()
- local mainp = Instance.new("Part",snake)
- mainp.Name = "head"
- mainp.FormFactor = "Custom"
- mainp.Size = Vector3.new(speed,speed,speed)
- mainp.Anchored = true
- mainp.BrickColor = headcolor
- mainp.CanCollide = cancollide
- mainp.CFrame = CFrame.new(0,base.Position.Y + base.Size.Y/2 + mainp.Size.Y/2,0)
- main = mainp
- end
- function createp(pos,name)
- --print("Creating a part named "..name)
- local p = Instance.new("Part",snake)
- p.Name = tostring(name)
- p.FormFactor = "Custom"
- p.Size = Vector3.new(speed,speed,speed)
- p.Anchored = true
- p.CanCollide = cancollide
- if color == true then
- color = false
- p.BrickColor = tailcolor1
- else
- color = true
- p.BrickColor = tailcolor2
- end
- p.CFrame = CFrame.new(pos.X,pos.Y,pos.Z)
- --game:GetService("Debris"):AddItem(p, 3)
- end
- function cont()
- createmp()
- count1 = 0
- for i = 1, 100 do
- dir = math.random(1,4)
- for t = 1,5 do
- count1 = count1 + 1
- wait(speed2)
- done = move(dir,count1)
- if done == true then
- else
- dir = math.random(1,4)
- end
- end
- end
- end
- function getlastpart(co)
- a = snake:GetChildren()
- smallest = co
- --print("Items in snake: "..#a)
- for i = 1,#a do
- c = a[i]
- --print(i.." item is "..c.Name)
- if c.Name ~= "head" then
- --print("not head")
- if tonumber(c.Name) < smallest then
- smallest = tonumber(c.Name)
- end
- end
- end
- lastpart = snake:FindFirstChild(tostring(smallest))
- end
- function move(dir,i)-- moves in the direction given
- --print(dir)
- getlastpart(i)
- if dir == 1 then
- if lastloc ~= Vector3.new(main.Position.X-speed,main.Position.Y,main.Position.Z) then
- lastloc = main.Position
- main.CFrame = CFrame.new(main.CFrame.X-speed,main.CFrame.Y,main.CFrame.Z)
- if lastpart ~= nil and i > length then
- lastpart:Remove()
- end
- createp(lastloc,i)
- return true
- else
- return false
- end
- elseif dir == 2 then
- if lastloc ~= Vector3.new(main.Position.X+speed,main.Position.Y,main.Position.Z) then
- lastloc = main.Position
- main.CFrame = CFrame.new(main.CFrame.X+speed,main.CFrame.Y,main.CFrame.Z)
- if lastpart ~= nil and i > length then
- lastpart:Remove()
- end
- createp(lastloc,i)
- return true
- else
- return false
- end
- elseif dir == 3 then
- if lastloc ~= Vector3.new(main.Position.X,main.Position.Y,main.Position.Z-speed) then
- lastloc = main.Position
- main.CFrame = CFrame.new(main.CFrame.X,main.CFrame.Y,main.CFrame.Z-speed)
- if lastpart ~= nil and i > length then
- lastpart:Remove()
- end
- createp(lastloc,i)
- return true
- else
- return false
- end
- elseif dir == 4 then
- if lastloc ~= Vector3.new(main.Position.X,main.Position.Y,main.Position.Z-speed) then
- lastloc = main.Position
- main.CFrame = CFrame.new(main.CFrame.X,main.CFrame.Y,main.CFrame.Z-speed)
- if lastpart ~= nil and i > length then
- lastpart:Remove()
- end
- createp(lastloc,i)
- return true
- else
- return false
- end
- else
- debug("Direction given is not correct!")
- end
- end
- function debug(msg) -- Shows a message
- local m = Instance.new("Hint",workspace)
- m.Text = msg
- m.Name = "Notification"
- game:GetService("Debris"):AddItem(m, 2)
- end
- cont()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement