Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define a table to store player scores
- local playerScores = {}
- -- Function to spawn a coin at a random position
- function spawnCoin()
- local coin = Instance.new("Part")
- coin.Size = Vector3.new(1, 1, 1)
- coin.Shape = Enum.PartType.Ball
- coin.Color = Color3.fromRGB(255, 223, 0)
- coin.Position = Vector3.new(math.random(-50, 50), 1, math.random(-50, 50))
- coin.Anchored = true
- coin.Name = "Coin"
- coin.Parent = workspace
- end
- -- Function to handle coin collection
- function collectCoin(player, coin)
- if player and coin and coin.Name == "Coin" then
- playerScores[player.Name] = (playerScores[player.Name] or 0) + 1
- coin:Destroy()
- print(player.Name .. " collected a coin! Score: " .. playerScores[player.Name])
- end
- end
- -- Event listener for player touch
- function onTouch(hit)
- local player = game.Players:GetPlayerFromCharacter(hit.Parent)
- if player then
- collectCoin(player, hit)
- end
- end
- -- Connect the touch event to the onTouch function
- function setupCoinTouchEvent(coin)
- coin.Touched:Connect(onTouch)
- function setupPlayer(player)
- playerScores[player.Name] = 0
- player.CharacterAdded:Connect(function(character)
- character.HumanoidRootPart.Touched:Connect(function(hit)
- onTouch(hit)
- end)
- end)
- print(player.Name .. " has joined the game.")
- end
- game.Players.PlayerAdded:Connect(setupPlayer)
- function startCoinSpawner()
- while true do
- wait(5)
- local coin = spawnCoin()
- setupCoinTouchEvent(coin)
- end
- end
- spawn(function()
- startCoinSpawner()
- end)
- function setupGame()
- for _, player in ipairs(game.Players:GetPlayers()) do
- setupPlayer(player)
- end
- end
- function createScoreGUI(player)
- local screenGui = Instance.new("ScreenGui")
- screenGui.Name = "ScoreGUI"
- screenGui.Parent = player:WaitForChild("PlayerGui")
- local scoreLabel = Instance.new("TextLabel")
- scoreLabel.Size = UDim2.new(0, 200, 0, 50)
- scoreLabel.Position = UDim2.new(0, 10, 0, 10)
- scoreLabel.Text = "Score: 0"
- scoreLabel.Parent = screenGui
- game:GetService("RunService").RenderStepped:Connect(function()
- scoreLabel.Text = "Score: " .. (playerScores[player.Name] or 0)
- end)
- end
- for _, player in ipairs(game.Players:GetPlayers()) do
- createScoreGUI(player)
- end
- game.Players.PlayerAdded:Connect(function(player)
- createScoreGUI(player)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement