Advertisement
Jun1212244565656

Script for a sword fight game on roblox(not complete)

Nov 27th, 2019
3,673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. 3:48 MAINSCRIPT:
  2. -- Define variables
  3.  
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5.  
  6. local ServerStorage = game:GetService("ServerStorage")
  7.  
  8. local MapsFolder = ServerStorage:WaitForChild("Maps")
  9.  
  10. local Status = ReplicatedStorage:WaitForChild("Status")
  11.  
  12. local GameLength = 60
  13.  
  14. local reward = 25
  15.  
  16. -- Game loop
  17.  
  18. while true do
  19.  
  20. Status.Value = "Waiting for enough players"
  21.  
  22. repeat wait() until game.Players.NumPlayers >= 2
  23.  
  24. Status.Value = "Intermission"
  25.  
  26. wait(5)
  27.  
  28. local plrs = {}
  29.  
  30. for i, player in pairs(game.Players:GetPlayers()) do
  31. if player then
  32. table.insert(plrs,player) -- Add each player into plrs table
  33. end
  34. end
  35.  
  36. wait(2)
  37.  
  38. local AvailableMaps = MapsFolder:GetChildren()
  39.  
  40. local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
  41.  
  42. Status.Value = ChosenMap.Name.." Chosen"
  43.  
  44. local ClonedMap = ChosenMap:Clone()
  45. ClonedMap.Parent = workspace
  46.  
  47. -- Teleport players to the map
  48.  
  49. local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
  50.  
  51. if not SpawnPoints then
  52. print("Spawnpoints not found!")
  53. end
  54.  
  55. local AvailableSpawnPoints = SpawnPoints:GetChildren()
  56.  
  57. for i, player in pairs(plrs) do
  58. if player then
  59. character = player.Character
  60.  
  61. if character then
  62. -- Teleport them
  63.  
  64. character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
  65. table.remove(AvailableSpawnPoints,1)
  66.  
  67.  
  68. -- Give them a sword
  69.  
  70. local Sword = ServerStorage.Sword:Clone()
  71. Sword.Parent = player.Backpack
  72.  
  73. local GameTag = Instance.new("BoolValue")
  74. GameTag.Name = "GameTag"
  75. GameTag.Parent = player.Character
  76.  
  77. else
  78. -- There is no character
  79. if not player then
  80. table.remove(plrs,i)
  81. end
  82. end
  83. end
  84. end
  85.  
  86.  
  87. Status.Value = "Get ready to play!"
  88.  
  89. wait(2)
  90.  
  91. for i = GameLength,0,-1 do
  92.  
  93. for x, player in pairs(plrs) do
  94. if player then
  95.  
  96. character = player.Character
  97.  
  98. if not character then
  99. -- Left the game
  100. table.remove(plrs,x)
  101. else
  102. if character:FindFirstChild("GameTag") then
  103. -- They are still alive
  104. print(player.Name.." is still in the game!")
  105. else
  106. -- They are dead
  107. table.remove(plrs,x)
  108. print(player.Name.." has been removed!")
  109. end
  110. end
  111. else
  112. table.remove(plrs,x)
  113. print(player.Name.." has been removed!")
  114. end
  115. end
  116.  
  117. Status.Value = "There are "..i.." seocnds remaining, and "..#plrs.." players left"
  118.  
  119. if #plrs == 1 then
  120. -- Last person standing
  121. Status.Value = "The winner is "..plrs[1].Name
  122. plrs[1].leaderstats.Bucks.Value = plrs[1].leaderstats.Bucks.Value + reward
  123. break
  124. elseif #plrs == 0 then
  125. Status.Value = "Nobody Won!"
  126. break
  127. elseif i == 0 then
  128. Status.Value = "Time up!"
  129. break
  130. end
  131.  
  132. wait(1)
  133. end
  134.  
  135. print("End of game")
  136.  
  137. wait(2)
  138.  
  139. for i, player in pairs(game.Players:GetPlayers()) do
  140. character = player.Character
  141.  
  142. if not character then
  143. -- Ignore them
  144. else
  145. if character:FindFirstChild("GameTag") then
  146. character.GameTag:Destroy()
  147. end
  148.  
  149. if player.Backpack:FindFirstChild("Sword") then
  150. player.Backpack.Sword:Destroy()
  151. end
  152.  
  153. if character:FindFirstChild("Sword") then
  154. character.Sword:Destroy()
  155. end
  156.  
  157. end
  158.  
  159. player:LoadCharacter()
  160. end
  161.  
  162. ClonedMap:Destroy()
  163.  
  164. Status.Value = "Game ended"
  165.  
  166. wait(2)
  167.  
  168. end SCRIPT FOR STATS:
  169. local dataStores = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
  170. local defaultCash = 10
  171. local playersLeft = 0
  172.  
  173. game.Players.PlayerAdded:Connect(function(player)
  174.  
  175. playersLeft = playersLeft + 1
  176.  
  177. local leaderstats = Instance.new("Folder")
  178. leaderstats.Name = "leaderstats"
  179. leaderstats.Parent = player
  180.  
  181. local bucks = Instance.new("IntValue")
  182. bucks.Name = "Bucks"
  183. bucks.Value = 0
  184. bucks.Parent = leaderstats
  185.  
  186. local playerData = Instance.new("Folder")
  187. playerData.Name = player.Name
  188. playerData.Parent = game.ServerStorage.PlayerData
  189.  
  190. local equipped = Instance.new("StringValue")
  191. equipped.Name = "Equipped"
  192. equipped.Parent = playerData
  193.  
  194. local inventory = Instance.new("Folder")
  195. inventory.Name = "Inventory"
  196. inventory.Parent = playerData
  197.  
  198.  
  199. player.CharacterAdded:Connect(function(character)
  200.  
  201. character.Humanoid.Died:Connect(function()
  202. -- Whenever somebody dies, this event will run
  203.  
  204. if character.Humanoid and character.Humanoid:FindFirstChild("creator") then
  205. game.ReplicatedStorage.Status.Value = tostring(character.Humanoid.creator.Value).." KILLED "..player.Name
  206. end
  207.  
  208. if character:FindFirstChild("GameTag") then
  209. character.GameTag:Destroy()
  210. end
  211.  
  212. player:LoadCharacter()
  213. end)
  214.  
  215. end)
  216.  
  217. -- Data Stores
  218.  
  219. local player_data
  220.  
  221. pcall(function()
  222. player_data = dataStores:GetAsync(player.UserId.."-Bucks") --1736284-Bucks
  223. end)
  224.  
  225. if player_data ~= nil then
  226. -- Player has saved data, load it in
  227. bucks.Value = player_data
  228. else
  229. -- New player
  230. bucks.Value = defaultCash
  231. end
  232.  
  233. end)
  234.  
  235. local bindableEvent = Instance.new("BindableEvent")
  236.  
  237. game.Players.PlayerRemoving:Connect(function(player)
  238.  
  239. pcall(function()
  240. dataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
  241. print("Saved")
  242. playersLeft = playersLeft - 1
  243. bindableEvent:Fire()
  244. end)
  245. end)
  246.  
  247. game:BindToClose(function()
  248. -- This will be triggered upon shutdown
  249. while playersLeft > 0 do
  250. bindableEvent.Event:Wait()
  251. end
  252. end)
  253.  
  254.  
  255. Good
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement