Fabrimat

tic tac toe remake v1.0

Oct 4th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Made by KrYn0MoRe
  2.  
  3. --DevSersponge made win and draw effects
  4.  
  5. local partfolder = Instance.new("Folder")
  6. partfolder.Parent = script
  7.  
  8. --[[
  9. 1,2,3
  10. 4,5,6
  11. 7,8,9
  12. ]]
  13.  
  14. local ways = {
  15.     {n1 = 1,n2 = 2,n3 = 3},
  16.     {n1 = 4,n2 = 5,n3 = 6},
  17.     {n1 = 7,n2 = 8,n3 = 9},
  18.     {n1 = 1,n2 = 5,n3 = 9},
  19.     {n1 = 3,n2 = 5,n3 = 7},
  20.     {n1 = 1,n2 = 4,n3 = 7},
  21.     {n1 = 2,n2 = 5,n3 = 8},
  22.     {n1 = 3,n2 = 6,n3 = 9},
  23. }
  24.  
  25. local plots = {}
  26. local parts = {}
  27. local win_songs = {
  28.     {453967617,1,0},
  29.     {1843322216,1,0},
  30.     {2969532905,1,0},
  31.     {341673299,1,25},
  32.     {682070138,1,0},
  33.     {2586407163,0.8,10},
  34. }
  35.  
  36. local playing = false
  37. local O,X,turn
  38. local multiplier = 3
  39. local s = 3
  40. local start_time = 5
  41. local turn_time = 10
  42. local win_time = 10
  43. local line_size = 0.05
  44. local tid = 0
  45. local maintext,board,stand
  46. local curline=nil
  47. local firstpart=nil
  48. local lastpart=nil
  49. local currentwinner=0
  50.  
  51.  
  52. function check()
  53.     local winner = nil
  54.     for i,way in pairs(ways) do
  55.         if winner~=nil then break end
  56.         pcall(function()
  57.             local v1,v2,v3 = plots[way.n1], plots[way.n2], plots[way.n3]
  58.             firstpart=v1
  59.             lastpart=v3
  60.             if (v1 and v2 and v3) then
  61.                 if (v1['plrval'].Value == X and v2['plrval'].Value == X and v3['plrval'].Value == X) then
  62.                     winner = X
  63.                     currentwinner=0
  64.                 elseif (v1['plrval'].Value == O and v2['plrval'].Value == O and v3['plrval'].Value == O) then
  65.                     winner = O
  66.                     currentwinner=1
  67.                 end
  68.             end
  69.         end)
  70.     end
  71.     if winner~=nil then
  72.         local distance = (firstpart.CFrame.p+Vector3.new(0,.5,0)-lastpart.CFrame.p+Vector3.new(0,.5,0)).magnitude
  73.         curline=Instance.new("Part",firstpart.Parent)
  74.         curline.Anchored=true
  75.         if currentwinner==0 then
  76.             curline.Color=Color3.new(1,0,0)
  77.         else
  78.             curline.Color=Color3.new(0,0,1)
  79.         end
  80.         curline.Material="Neon"
  81.         curline.Size=Vector3.new(.1,.5,.5)
  82.         curline.CFrame=CFrame.new(lastpart.CFrame.p+Vector3.new(0,.5,0),firstpart.CFrame.p+Vector3.new(0,.5,0))*CFrame.Angles(0,math.rad(90),0)
  83.        
  84.         --sine
  85.         spawn(function()
  86.             for i=1,#parts do
  87.                 if parts[i]==nil then return end
  88.                 if parts[i]:IsA("BasePart") then
  89.                     spawn(function()
  90.                         local currentpart=parts[i]
  91.                         local pos=currentpart.Position
  92.                         for z=1,100 do    
  93.                             local delta=z/100
  94.                             pcall(function()
  95.                                 currentpart.Position=pos+Vector3.new(0,1*math.sin(z*0.25),0)
  96.                                 wait()
  97.                             end)
  98.                         end
  99.                         for z=1,20 do      
  100.                             local delta=z/20
  101.                             pcall(function()
  102.                                 currentpart.Position=p.Position:lerp(pos,delta)
  103.                                 wait()
  104.                             end)
  105.                         end
  106.                         currentpart.Position=pos
  107.                         coroutine.yield()
  108.                     end)
  109.                 end
  110.                 wait(.1)
  111.             end
  112.             coroutine.yield()
  113.         end)
  114.        
  115.         --sine
  116.         spawn(function()
  117.             for i=1,150 do
  118.                 pcall(function()         
  119.                     local delta=i/110
  120.                     curline.Size = curline.Size:lerp(Vector3.new(distance+2,.5,.5),delta) --/ 2
  121.                     curline.CFrame = curline.CFrame:lerp(CFrame.new(firstpart.CFrame.p+Vector3.new(0,.5,0),lastpart.CFrame.p+Vector3.new(0,.5,0)) *CFrame.new(0, 0, -distance/2)*CFrame.Angles(math.rad(0),math.rad(90),math.rad(0)),delta)
  122.                     wait()
  123.                 end)
  124.             end
  125.             coroutine.yield()
  126.         end)
  127.     end
  128.     return winner
  129. end
  130.  
  131. function set_turn(plr)
  132.     local cid = tid
  133.     local name = plr.Name
  134.     turn = plr
  135.     spawn(function()
  136.         local _time = turn_time
  137.         repeat
  138.             _time -= 1;
  139.             wait(1);
  140.             if playing then
  141.                 update(name .. "'s Turn. (" .. _time .. ')')
  142.             end
  143.         until 0 >= _time or tid ~= cid or not playing
  144.         if not playing then
  145.             return
  146.         end
  147.         if tid == cid then
  148.             if turn == X then
  149.                 set_turn(O)
  150.             else
  151.                 set_turn(X)
  152.             end
  153.         end
  154.     end)
  155. end
  156.  
  157. function make(p1,p2,p3)
  158.     board = Instance.new("Model")
  159.     board.Parent = partfolder
  160.     local total = 0
  161.     parts = {}
  162.     for column = 1,multiplier do
  163.         for row = 1,multiplier do
  164.             local part = Instance.new("Part")
  165.             part.Color = Color3.new(1,1,1)
  166.             part.Anchored = true
  167.             part.CanCollide = true
  168.             part.Locked = true
  169.             part.Material = Enum.Material.SmoothPlastic
  170.             part.Size = Vector3.new(s,1,s)
  171.             part.CFrame = CFrame.new(p1+((s+line_size*2)*row),p2,p3+((s+line_size*2)*column))*CFrame.Angles(0,math.rad(90),0)
  172.             part.Name = tostring(((column-1)*multiplier)+row)
  173.             part.Parent = board
  174.             table.insert(parts,part)
  175.             local grid = Instance.new("SelectionBox")
  176.             grid.LineThickness = line_size
  177.             grid.Color3 = Color3.new(0,0,0)
  178.             grid.Adornee = part
  179.             grid.Parent = part
  180.             local plrval = Instance.new("ObjectValue",part)
  181.             plrval.Name = "plrval"
  182.             local SurfaceGui0 = Instance.new("SurfaceGui")
  183.             local TextBox1 = Instance.new("TextBox")
  184.             SurfaceGui0.Parent = part
  185.             SurfaceGui0.LightInfluence = 1
  186.             SurfaceGui0.Face = Enum.NormalId.Top
  187.             SurfaceGui0.PixelsPerStud = 50
  188.             SurfaceGui0.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
  189.             SurfaceGui0.ClipsDescendants = true
  190.             SurfaceGui0.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  191.             TextBox1.Parent = SurfaceGui0
  192.             TextBox1.Size = UDim2.new(1, 0, 1, 0)
  193.             TextBox1.BackgroundTransparency = 1
  194.             TextBox1.Text = ""
  195.             TextBox1.TextScaled = true
  196.             local clicker = Instance.new("ClickDetector")
  197.             clicker.MaxActivationDistance = 15
  198.             clicker.Parent = part
  199.             local clicked = false
  200.             clicker.MouseClick:Connect(function(plr)
  201.                 if plr and turn == plr and not clicked and playing then
  202.                     clicked = true
  203.                     tid += 1
  204.                     local s = Instance.new("Sound")
  205.                     s.SoundId = 'rbxassetid://452267918'
  206.                     s.Volume = 1
  207.                     s.Parent = part
  208.                     s:Play()
  209.                     if plr == X and turn == X then
  210.                         clicker:Destroy()
  211.                         TextBox1.Text = "X"
  212.                         plrval.Value = X
  213.                         set_turn(O)
  214.                         game:GetService("TweenService"):Create(grid,TweenInfo.new(1),{Color3 = Color3.new(1,0,0)}):Play()
  215.                     elseif plr == O and turn == O then
  216.                         clicker:Destroy()
  217.                         TextBox1.Text = "O"
  218.                         plrval.Value = O
  219.                         set_turn(X)
  220.                         game:GetService("TweenService"):Create(grid,TweenInfo.new(1),{Color3 = Color3.new(0,0,1)}):Play()
  221.                     end
  222.                     total += 1
  223.                     plots[tonumber(part.Name)] = part
  224.                     local winner = check()
  225.                     if winner then
  226.                         playing = false
  227.                         local song = win_songs[math.random(1,#win_songs)]
  228.                         local win_s = Instance.new("Sound")
  229.                         win_s.SoundId = 'rbxassetid://' .. song[1]
  230.                         win_s.PlaybackSpeed = song[2]
  231.                         win_s.TimePosition = song[3]
  232.                         win_s.Volume = 3
  233.                         win_s.Parent = part
  234.                         win_s:Resume()
  235.                         for i = 1,win_time-1 do
  236.                             update(winner.Name .. " Won! (" .. tostring(win_time-i) .. ')')
  237.                             wait(1)
  238.                         end
  239.                         win_s:Destroy()
  240.                         start()
  241.                     elseif total >= multiplier^2 then
  242.                         playing = false
  243.                         spawn(function()
  244.                             for i=1,#parts do
  245.                                 local currentpart=parts[i]
  246.                                 spawn(function()
  247.                                     local pos=currentpart.Position
  248.                                     for z=1,100 do    
  249.                                         local delta=z/100
  250.                                         pcall(function()
  251.                                             currentpart.Size=currentpart.Size:lerp(Vector3.new(.1,.1,.1),delta)
  252.                                             currentpart.Rotation=currentpart.Rotation:lerp(Vector3.new(0,math.rad(90),math.rad(180)),delta)
  253.                                             wait()
  254.                                         end)
  255.                                     end
  256.                                 end)
  257.                             end
  258.                         end)
  259.                         for i = 1,win_time-1 do
  260.                             update("Draw. (" .. tostring(win_time-i) .. ')')
  261.                             wait(1)
  262.                         end
  263.                         start()
  264.                     end
  265.                 end
  266.             end)
  267.         end
  268.     end
  269. end
  270.  
  271. function prepare()
  272.     stand = Instance.new("Model")
  273.     local Part1 = Instance.new("Part")
  274.     local Part2 = Instance.new("Part")
  275.     local SurfaceGui3 = Instance.new("SurfaceGui")
  276.     local TextBox4 = Instance.new("TextBox")
  277.     local Part5 = Instance.new("Part")
  278.     local SurfaceGui6 = Instance.new("SurfaceGui")
  279.     local TextBox7 = Instance.new("TextBox")
  280.     local Part8 = Instance.new("Part")
  281.     local SurfaceGui9 = Instance.new("SurfaceGui")
  282.     maintext = Instance.new("TextBox")
  283.     stand.Name = "choose"
  284.     stand.Parent = partfolder
  285.     Part1.Parent = stand
  286.     Part1.CFrame = CFrame.new(-30.9599934, 4.33500385, 89.4899979, 1, 0, 0, 0, 1, 0, 0, 0, 1)
  287.     Part1.Position = Vector3.new(-30.9599934, 4.33500385, 89.4899979)
  288.     Part1.Color = Color3.new(0.0156863, 0.686275, 0.92549)
  289.     Part1.Size = Vector3.new(17.0199986, 8.61000061, 2)
  290.     Part1.Anchored = true
  291.     Part1.BottomSurface = Enum.SurfaceType.Smooth
  292.     Part1.BrickColor = BrickColor.new("Cyan")
  293.     Part1.Locked = true
  294.     Part1.TopSurface = Enum.SurfaceType.Smooth
  295.     Part1.brickColor = BrickColor.new("Cyan")
  296.     Part2.Name = "X"
  297.     Part2.Parent = stand
  298.     Part2.CFrame = CFrame.new(-35.9699898, 2.83500385, 89.9899979, 1, 0, 0, 0, 1, 0, 0, 0, 1)
  299.     Part2.Position = Vector3.new(-35.9699898, 2.83500385, 89.9899979)
  300.     Part2.Color = Color3.new(0.686275, 0.866667, 1)
  301.     Part2.Size = Vector3.new(5, 3.61000061, 2)
  302.     Part2.Anchored = true
  303.     Part2.BottomSurface = Enum.SurfaceType.Smooth
  304.     Part2.BrickColor = BrickColor.new("Pastel light blue")
  305.     Part2.Locked = true
  306.     Part2.TopSurface = Enum.SurfaceType.Smooth
  307.     Part2.brickColor = BrickColor.new("Pastel light blue")
  308.     SurfaceGui3.Parent = Part2
  309.     SurfaceGui3.LightInfluence = 1
  310.     SurfaceGui3.Face = Enum.NormalId.Back
  311.     SurfaceGui3.ClipsDescendants = true
  312.     SurfaceGui3.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  313.     SurfaceGui3.PixelsPerStud = 50
  314.     SurfaceGui3.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
  315.     TextBox4.Parent = SurfaceGui3
  316.     TextBox4.Size = UDim2.new(1, 0, 1, 0)
  317.     TextBox4.BackgroundColor = BrickColor.new("Institutional white")
  318.     TextBox4.BackgroundColor3 = Color3.new(1, 1, 1)
  319.     TextBox4.BackgroundTransparency = 1
  320.     TextBox4.Font = Enum.Font.SourceSans
  321.     TextBox4.FontSize = Enum.FontSize.Size14
  322.     TextBox4.Text = "X"
  323.     TextBox4.TextColor = BrickColor.new("Really black")
  324.     TextBox4.TextColor3 = Color3.new(0, 0, 0)
  325.     TextBox4.TextScaled = true
  326.     TextBox4.TextSize = 14
  327.     TextBox4.TextWrap = true
  328.     TextBox4.TextWrapped = true
  329.     Part5.Name = "O"
  330.     Part5.Parent = stand
  331.     Part5.CFrame = CFrame.new(-25.9699898, 2.83500385, 89.9899979, 1, 0, 0, 0, 1, 0, 0, 0, 1)
  332.     Part5.Position = Vector3.new(-25.9699898, 2.83500385, 89.9899979)
  333.     Part5.Color = Color3.new(0.686275, 0.866667, 1)
  334.     Part5.Size = Vector3.new(5, 3.61000061, 2)
  335.     Part5.Anchored = true
  336.     Part5.BottomSurface = Enum.SurfaceType.Smooth
  337.     Part5.BrickColor = BrickColor.new("Pastel light blue")
  338.     Part5.Locked = true
  339.     Part5.TopSurface = Enum.SurfaceType.Smooth
  340.     Part5.brickColor = BrickColor.new("Pastel light blue")
  341.     SurfaceGui6.Parent = Part5
  342.     SurfaceGui6.LightInfluence = 1
  343.     SurfaceGui6.Face = Enum.NormalId.Back
  344.     SurfaceGui6.ClipsDescendants = true
  345.     SurfaceGui6.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  346.     SurfaceGui6.PixelsPerStud = 50
  347.     SurfaceGui6.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
  348.     TextBox7.Parent = SurfaceGui6
  349.     TextBox7.Size = UDim2.new(1, 0, 1, 0)
  350.     TextBox7.BackgroundColor = BrickColor.new("Institutional white")
  351.     TextBox7.BackgroundColor3 = Color3.new(1, 1, 1)
  352.     TextBox7.BackgroundTransparency = 1
  353.     TextBox7.Font = Enum.Font.SourceSans
  354.     TextBox7.FontSize = Enum.FontSize.Size14
  355.     TextBox7.Text = "O"
  356.     TextBox7.TextColor = BrickColor.new("Really black")
  357.     TextBox7.TextColor3 = Color3.new(0, 0, 0)
  358.     TextBox7.TextScaled = true
  359.     TextBox7.TextSize = 14
  360.     TextBox7.TextWrap = true
  361.     TextBox7.TextWrapped = true
  362.     Part8.Parent = stand
  363.     Part8.CFrame = CFrame.new(-30.9699936, 6.79000378, 89.4899902, 1, 0, 0, 0, 1, 0, 0, 0, 1)
  364.     Part8.Position = Vector3.new(-30.9699936, 6.79000378, 89.4899902)
  365.     Part8.Color = Color3.new(0.686275, 0.866667, 1)
  366.     Part8.Transparency = 1
  367.     Part8.Size = Vector3.new(15, 3.70000076, 2)
  368.     Part8.Anchored = true
  369.     Part8.BottomSurface = Enum.SurfaceType.Smooth
  370.     Part8.BrickColor = BrickColor.new("Pastel light blue")
  371.     Part8.CanCollide = false
  372.     Part8.Locked = true
  373.     Part8.TopSurface = Enum.SurfaceType.Smooth
  374.     Part8.brickColor = BrickColor.new("Pastel light blue")
  375.     SurfaceGui9.Parent = Part8
  376.     SurfaceGui9.LightInfluence = 1
  377.     SurfaceGui9.Face = Enum.NormalId.Back
  378.     SurfaceGui9.ClipsDescendants = true
  379.     SurfaceGui9.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  380.     SurfaceGui9.PixelsPerStud = 50
  381.     SurfaceGui9.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
  382.     maintext.Parent = SurfaceGui9
  383.     maintext.Size = UDim2.new(1, 0, 1, 0)
  384.     maintext.BackgroundColor = BrickColor.new("Institutional white")
  385.     maintext.BackgroundColor3 = Color3.new(1, 1, 1)
  386.     maintext.BackgroundTransparency = 1
  387.     maintext.Font = Enum.Font.SourceSans
  388.     maintext.FontSize = Enum.FontSize.Size14
  389.     maintext.Text = ([[Tic Tac Toe
  390.     Choose a player!]])
  391.     maintext.TextColor = BrickColor.new("Really black")
  392.     maintext.TextColor3 = Color3.new(0, 0, 0)
  393.     maintext.TextScaled = true
  394.     maintext.TextSize = 14
  395.     maintext.TextWrap = true
  396.     maintext.TextWrapped = true
  397.     local clickX,clickO = Instance.new("ClickDetector"),Instance.new("ClickDetector")
  398.     clickX.Parent = Part2
  399.     clickO.Parent = Part5
  400.     clickX.MaxActivationDistance = 20
  401.     clickO.MaxActivationDistance = 20
  402.     clickX.MouseClick:Connect(function(plr)
  403.         if plr and plr ~= (O or X) then else return end
  404.         X = plr
  405.         TextBox4.Text = plr.Name .. "(X) Selected!"
  406.         clickX:Destroy()
  407.     end)
  408.     clickO.MouseClick:Connect(function(plr)
  409.         if plr and plr ~= (O or X) then else return end
  410.         O = plr
  411.         TextBox7.Text = plr.Name .. "(O) Selected!"
  412.         clickO:Destroy()
  413.     end)
  414.     repeat wait(1) until (X and O)
  415.     for i = 1,start_time do
  416.         update("Starting in " .. tostring(start_time-i) .. "..")
  417.         wait(1)
  418.     end
  419.     playing = true
  420.     set_turn(X)
  421. end
  422.  
  423. function update(txt)
  424.     if maintext and txt then
  425.         maintext.Text = txt
  426.     end
  427. end
  428.  
  429. function start()
  430.     partfolder:ClearAllChildren()
  431.     plots = {}
  432.     playing = false
  433.     turn = nil
  434.     X = nil
  435.     O = nil
  436.     tid = 0
  437.     prepare()
  438.     make(-37, 0.5, 93)
  439. end
  440.  
  441. start()
Add Comment
Please, Sign In to add comment