Advertisement
ohusq

Untitled

Jun 10th, 2024
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | Source Code | 0 0
  1. -- Define a table to store player scores
  2. local playerScores = {}
  3.  
  4. -- Function to spawn a coin at a random position
  5. function spawnCoin()
  6.     local coin = Instance.new("Part")
  7.     coin.Size = Vector3.new(1, 1, 1)
  8.     coin.Shape = Enum.PartType.Ball
  9.     coin.Color = Color3.fromRGB(255, 223, 0)
  10.     coin.Position = Vector3.new(math.random(-50, 50), 1, math.random(-50, 50))
  11.     coin.Anchored = true
  12.     coin.Name = "Coin"
  13.     coin.Parent = workspace
  14. end
  15.  
  16. -- Function to handle coin collection
  17. function collectCoin(player, coin)
  18.     if player and coin and coin.Name == "Coin" then
  19.         playerScores[player.Name] = (playerScores[player.Name] or 0) + 1
  20.         coin:Destroy()
  21.         print(player.Name .. " collected a coin! Score: " .. playerScores[player.Name])
  22.     end
  23. end
  24.  
  25. -- Event listener for player touch
  26. function onTouch(hit)
  27.     local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  28.     if player then
  29.         collectCoin(player, hit)
  30.     end
  31. end
  32.  
  33. -- Connect the touch event to the onTouch function
  34. function setupCoinTouchEvent(coin)
  35.     coin.Touched:Connect(onTouch)
  36. function setupPlayer(player)
  37.     playerScores[player.Name] = 0
  38.     player.CharacterAdded:Connect(function(character)
  39.         character.HumanoidRootPart.Touched:Connect(function(hit)
  40.             onTouch(hit)
  41.         end)
  42.     end)
  43.     print(player.Name .. " has joined the game.")
  44. end
  45. game.Players.PlayerAdded:Connect(setupPlayer)
  46. function startCoinSpawner()
  47.     while true do
  48.         wait(5)
  49.         local coin = spawnCoin()
  50.         setupCoinTouchEvent(coin)
  51.     end
  52. end
  53. spawn(function()
  54.     startCoinSpawner()
  55. end)
  56. function setupGame()
  57.     for _, player in ipairs(game.Players:GetPlayers()) do
  58.         setupPlayer(player)
  59.     end
  60. end
  61. function createScoreGUI(player)
  62.     local screenGui = Instance.new("ScreenGui")
  63.     screenGui.Name = "ScoreGUI"
  64.     screenGui.Parent = player:WaitForChild("PlayerGui")
  65.     local scoreLabel = Instance.new("TextLabel")
  66.     scoreLabel.Size = UDim2.new(0, 200, 0, 50)
  67.     scoreLabel.Position = UDim2.new(0, 10, 0, 10)
  68.     scoreLabel.Text = "Score: 0"
  69.     scoreLabel.Parent = screenGui
  70.     game:GetService("RunService").RenderStepped:Connect(function()
  71.         scoreLabel.Text = "Score: " .. (playerScores[player.Name] or 0)
  72.     end)
  73. end
  74. for _, player in ipairs(game.Players:GetPlayers()) do
  75.     createScoreGUI(player)
  76. end
  77. game.Players.PlayerAdded:Connect(function(player)
  78.     createScoreGUI(player)
  79. end)
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement