Advertisement
KrYn0MoRe

tic tac toe remake v1.0

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