-- Touch-to-Collect Coin: touch handler.
--
-- Goes inside a Script that is a child of the Coin Part in
-- Workspace. When a player walks into the coin, their Coins value
-- goes up by 1 and the coin is destroyed.

local Players = game:GetService("Players")
local coin = script.Parent

local collected = false

coin.Touched:Connect(function(other)
    if collected then return end

    -- The toucher is a Part; the character it belongs to is its parent.
    local character = other.Parent
    if character == nil then return end

    local player = Players:GetPlayerFromCharacter(character)
    if player == nil then return end

    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats == nil then return end

    local coins = leaderstats:FindFirstChild("Coins")
    if coins == nil then return end

    collected = true
    coins.Value = coins.Value + 1
    coin:Destroy()
end)
