Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Coin that adds to leaderboard on contact?

I am working on a place that I would like to have a coin that spawns every so often and on contact adds money/cash to leaderboard? (I am not a very good scripter, so I normally just pirate stuff from models and try to patch it together. I guess I was just hoping for a full script.

2 answers

✓ Accepted answer

Ok. First of all, this is not a request site, so please do not think it is. However, I will help you just this once. So first off you need leaderboards:

game.Players.PlayerAdded:connect(function(plr) -- This line goes off once a player joins the game. That player can be refferred to as 'plr' through this function
	local stats = Instance.new('IntValue',plr) -- leaderstats, children's value desplayed to all players
	stats.Name = 'leaderstats'
	local coins = Instance.new('IntValue', stats)
	coins.Name = 'Coins' -- Chnge 'Coins' to what you want to name to be
	coins.Value = 0 -- Change this to be the Value theyhave when they join
end)

That is a very simple leaderboard script. If you want to save it when they join and leave, I'll update it with explanations of DataSTores. Now, the coin. Lets say you make a part, which is meant to be the coin. You now need a script like this in the part:

local debounce = false
function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		if debounce == false then
			debounce = true
			local thisplr = game.Players:FindFirstChild(h.Parent.Name)
			local stats = thisplr.leaderstats
			local score = stats.Coins-- change this to what the thing is called now, if you changed the name of Coins
			if (score~=nil) then
				local mon = math.random(250,1000) -- Makes a random number
				score.Value = score.Value + mon -- change this to the number of coins they get if you don't want it to be random. You didn't specify what you wanted.
				wait()
				local item = script.Parent
				item:ClearAllChildren()
				item:Destroy()
			end			
		end
	end
end

script.Parent.Touched:connect(onTouched)

The script above is very basic. Once touched by a player, the parent of the script will be removed and the player will receive coins. (In this case, a random number that could be 250-1000) - Now the last part, which is spawning the part. Put the part in Lighting or ReplicatedStorage. Now this script will be in workspace.

local item = game.Lighting.Part -- find the item
while wait(60) do -- change the number to what you want
	local clone = item:Clone()
	clone.Parent = workspace
	-- add a line to choose position if you want to
end

Now, all these scripts are very simple and should be improved. I typed them without opening studio, simply to help you understand the concept. Good luck from here! (I typed this out in 10 minutes while on the site, typos and silly mistakes are bound to happen) Have a great day.

4 comments

0

Thank you so much! The only thing that I couldn't get to work was that spawning of the Part, how do you give it a specific area to spawn?

0

You will have to adjust the position. If you're using a model, you can use MoveTo. Or if just a part, then you can change it's Vector pretty much. Use the wiki

0

Also note that my last script has a problem. it should be game.workspace not just workspace

0

Another thing how would you make it only spawn one block and only respawn after you pick it up again.

Assuming you already have the leaderstat "Coins"

amnt = 1


function onTouched(part)

	local h = part.Parent:FindFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:FindFirstChild("leaderstats")
			if (stats~=nil) then
				local score = stats:FindFirstChild("Coins")
				if (score~=nil) then
					score.Value = score.Value + amnt
				end
			end
		end
		script.Parent:remove()
	end
end





script.Parent.Touched:connect(onTouched)

This discussion is preserved as it was. New answers, comments, and votes are closed.