Advertisement
Null_Cat

Minesweeper (Client) v1/v2 (Roblox)

Jan 27th, 2019
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. --alright minesweeper time
  2. --kill me
  3. --[[ This is grabbing the play and waiting for controls and stuff.
  4. mostly just mobile stuff but whatever]]--
  5.  
  6. local plr = game:GetService"Players".LocalPlayer -- Grabs player
  7. local chr = plr.Character -- Grabs the player's Model
  8. if workspace:FindFirstChild(plr.Name.."MinesweeperClient") then error("The local is already being ran. If you believe this is an error, run the following:\n\"c/owner.Character:FindFirstChild(plr.Name..\"MinesweeperClient\"):Destroy()\"") return end
  9. local storage = game:GetService"ReplicatedStorage" -- Grab the ReplicatedStorage which contains the RemoteEvent
  10. local remote = storage.MineRemote -- Grab the RemoteEVent
  11. script.Name = plr.Name.."MinesweeperClient"
  12.  
  13. local idForPlr = 0 -- This will be grabbed later. It will help in placing the boards
  14. local ignoreMessages = false
  15. local widthChatRequest, heightChatRequest, mineChatRequest = 2, 2, 1 -- This will come in the "Chatted" Event
  16. local beginner, intermediate, expert = { -- The width, height, and mine count for each difficulty
  17.     ["width"] = 8,
  18.     ["height"] = 8,
  19.     ["mines"] = 10
  20. },{
  21.     ["width"] = 16,
  22.     ["height"] = 16,
  23.     ["mines"] = 40
  24. },{
  25.     ["width"] = 16,
  26.     ["height"] = 30,
  27.     ["mines"] = 99
  28. }
  29.  
  30. function roundNum(num)
  31.   local baseNum, newNum
  32.   if num > 0 then
  33.     baseNum = math.floor(num)
  34.     newNum = num - baseNum
  35.     if newNum >= 0.5 then
  36.       return math.ceil(num)
  37.     elseif newNum < 0.5 then
  38.       return math.floor(num)
  39.     end
  40.   else
  41.     baseNum = math.ceil(num)
  42.     newNum = num - baseNum
  43.     if newNum > -0.5 then
  44.       return math.ceil(num)
  45.     elseif newNum <= -0.5 then
  46.       return math.floor(num)
  47.     end
  48.   end
  49. end
  50.  
  51. function returnID()
  52.     remote:FireServer("ID",0,0,0,idForPlr) -- Send the ID
  53.     idForPlr = 0
  54. end
  55.  
  56. plr.Chatted:Connect(function(msg)
  57.     if msg:sub(1,3):lower() == "/m " then
  58.         local cmd = msg:sub(4)
  59.         if not ignoreMessages then
  60.             ignoreMessages = true
  61.             if cmd:sub(1,1) == "b" then
  62.                 widthChatRequest, heightChatRequest, mineChatRequest = beginner.width, beginner.height, beginner.mines -- Sets beginner difficulty
  63.                 remote:FireServer("Grab", nil, nil, nil, nil) -- Requests an ID
  64.             elseif cmd:sub(1,1) == "i" then
  65.                 widthChatRequest, heightChatRequest, mineChatRequest = intermediate.width, intermediate.height, intermediate.mines -- Sets intermediate difficulty
  66.                 remote:FireServer("Grab", nil, nil, nil, nil) -- Requests an ID
  67.             elseif cmd:sub(1,1) == "e" then
  68.                 widthChatRequest, heightChatRequest, mineChatRequest = expert.width, expert.height, expert.mines -- Sets expert difficulty
  69.                 remote:FireServer("Grab", nil, nil, nil, nil) -- Requests an ID
  70.             elseif cmd:sub(1,1) == "c" then -- Custom Board
  71.         local customVariables = cmd:sub(2) -- grab ONLY the custom variables
  72.         local count = 0 -- This will help in sorting the request
  73.         for i in customVariables:gmatch("%S+") do -- Splits the variables into their own seperate parts
  74.           count = count + 1 -- Adds one to the count, which changes which part to change about the board
  75.           local a = tonumber(i) -- Changes the string to a number
  76.           if count == 1 then -- 1 is requesting width
  77.             if a <= 32 and a > 7 then
  78.               widthChatRequest = a
  79.             elseif a < 7 then
  80.               warn("There were less than 7 width in the field. Setting to 7...")
  81.               widthChatRequest = 7
  82.             else
  83.               warn("The width of the board is too big! Setting to 32...")
  84.               widthChatRequest = 32
  85.             end
  86.           elseif count == 2 then -- 2 is requesting height
  87.             if a <= 32 and a > 7 then
  88.               heightChatRequest = a
  89.             elseif a < 7 then
  90.               warn("There were less than 7 height in the field. Setting to 7...")
  91.               heightChatRequest = 7
  92.             else
  93.               warn("The height of the board is too big! Setting to 32...")
  94.               heightChatRequest = 32
  95.             end
  96.           elseif count == 3 then -- 3 is requesting mine count
  97.             local maxMines = roundNum((widthChatRequest * heightChatRequest) / 1.25) -- This is to make it so there isn't more mines than the board itself
  98.             if a <= maxMines then
  99.               mineChatRequest = a
  100.             elseif a < 0 then
  101.               warn("There were less than 1 mine in the field. Setting to 1...")
  102.               mineChatRequest = 1
  103.             else
  104.               warn("The mine count is too much! Setting to "..maxMines.."...")
  105.               mineChatRequest = maxMines
  106.             end
  107.           end
  108.         end
  109.         remote:FireServer("Grab", nil, nil, nil, nil) -- Requests the ID
  110.       end
  111.         else
  112.             if cmd:sub(1,1) == "r" then -- Restart Minesweeper
  113.                 returnID()
  114.                 ignoreMessages = false
  115.             end
  116.         end
  117.     end
  118. end)
  119.  
  120.  
  121. remote.OnClientEvent:Connect(function(action, newID) -- Server to Client stuff
  122.     if action == "Sending" then -- When the server gives an ID
  123.         idForPlr = newID -- Store the ID
  124.         remote:FireServer("Board", widthChatRequest,heightChatRequest,mineChatRequest,idForPlr) -- Then request a board
  125.         warn("Jump to start. Do not walk off the invisible platform you are on.")
  126.         print("0-Transparent") -- Gives the player instructions on to what each color represents
  127.         print("1-Blue")
  128.         print("2-Green")
  129.         print("3-Red")
  130.         print("4-Purple")
  131.         print("5-Crimson")
  132.         print("6-Turquoise")
  133.         print("7-Black")
  134.         print("8-Gray")
  135.     elseif action == "Recieving" then -- When the server requests for the ID
  136.         returnID()
  137.         ignoreMessages = false
  138.     end
  139. end)
  140.  
  141. chr:FindFirstChildOfClass("Humanoid").Died:Connect(function() -- If the humanoid dies, return the ID. Failsafe.
  142.     returnID()
  143.     script:Destroy()
  144. end)
  145.  
  146. print("Prefix: /m\nb, i, e run the beginner, intermediate, and expert boards respectively.\nc [w] [h] [m] to set a custom board [w]*[h] big with [m] mines.\nr to reset your board.\nt to teleport back to your board.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement