Skinstashery
Roblox
Home / Roblox / Studio script library / Coin pickup
RB-004

Roblox coin pickup script

A coin that adds to the player's leaderstats when touched, then comes back after a while.

Script type
Script
Where it goes
Inside the Coin Part
Category
Player data and money

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Coin pickup: gives coins and respawns after a delay.
-- Needs the Leaderstats (coins) script so player.leaderstats.Coins exists.

local COIN_VALUE = 1
local RESPAWN_TIME = 10       -- seconds before the coin comes back. Use 0 to remove it for good.

local Players = game:GetService("Players")

local coin = script.Parent
local collected = false

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

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then
		return
	end

	local leaderstats = player:FindFirstChild("leaderstats")
	local coins = leaderstats and leaderstats:FindFirstChild("Coins")
	if not coins then
		return
	end

	collected = true
	coins.Value += COIN_VALUE

	-- Hide the coin.
	coin.Transparency = 1
	coin.CanTouch = false

	if RESPAWN_TIME <= 0 then
		coin:Destroy()
		return
	end

	task.wait(RESPAWN_TIME)

	coin.Transparency = 0
	coin.CanTouch = true
	collected = false
end)

How to set it up

  1. Make sure the Leaderstats (coins) script is already in ServerScriptService.
  2. Insert a Part, make it a yellow cylinder or a flat disc, and set Anchored to true. Turn CanCollide off.
  3. Add a Script inside the Part and paste the code.
  4. Copy and paste the coin around the map. Play and collect them.

Change it

More in Player data and money

All Roblox scripts

More Roblox tools