Getting currency to add when player touches the part

Hello!

I am trying to script where if a player touches a part the currency (Clicks) adds one to the players leaderstats but I am not quite sure how to do that. Any solutions?

Script:

	local Player = game:GetService("Players")
	
	if Player then
		task.wait(1)
		Player.Data.PlayerData.Currency = Player.Data.PlayerData.Currency + 1
	end
end)

Firstly, if you don’t have a leaderboard, you should set one up, using the script:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats name here"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue")
	value.Name = "Value here"
	value.Value = 0
	value.Parent = leaderstats
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

Now’s your question, you should call an .Touched event whenever a player touch the part, and on that event, it’ll add values to your leaderstat.

local Players = game:GetService("Players")

local part = script.Parent

local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local player = Players:GetPlayerFromCharacter(partParent)
	local leaderstats = player and player:FindFirstChild("Name here")
	local value = leaderstats and leaderstats:FindFirstChild("Value here")

	if value then
		-- destroy the part(optional)
		-- goldChunk:Destroy()

		-- Update the player's leaderboard stat
		value.Value += 10
	end
end

part.Touched:Connect(onPartTouch)

If you are confused, feel free to ask me!

3 Likes

What is the goldchunk on the last line?

You can know when a player clicks a part by using a ClickDetector:
https://create.roblox.com/docs/reference/engine/classes/ClickDetector

There are code snippets and explanations of relevant code on the doc linked above.

This can be done by connecting the event of the click detector to your data storing code.

The fix would be to switch out goldChunk with the part variable.

Based on the code, it seems goldChunk is a Part/BasePart since it appears to have a .Touched event. goldChunk isn’t defined in the code though, so I assume it was meant to be what @BaconVXD defines as part in the code (script.Parent). Maybe a part of a personal project and forgot to change it fully.

2 Likes

It’s the part, sorry for that mistake, like what @Reditect say, if you want to make the player click the part instead of touching it, you gotta add a ClickDetector and changes the part.Touced to:

part.Clickdetectornamehere.MouseClick:Connect(eventnamehere)
1 Like

Well basicly what I am trying to do is make a AFK Grinder where every second you get a certain amount of clicks I have everything I needed to do but I needed extra help. If you also wanted to come in the game with me you can to solve this problem together.

In that case, you should probably do a repeated function which add values into the player’s stats every few delay or something. Do you need the script? if you do, i’ll do some testing and post it here

Yea I do need a script for my roblox game. Also a if you can, can you come into my game but you don’t have to but It really helps.

Ok, to clarify:
the player gets the currency via touching or clicking the part? Just so i know

Yes the player gets currency when touching the part

Ok @planeboy2021 , here’s a simpler script to help you with this.

Setup

game.Players.PlayerAdded:connect(function(plr)
	local f = Instance.new("Folder", plr)
	f.Name = "leaderstats"
	local coins = Instance.new("IntValue", f)
	coins.Name = "Clicks"
	coins.Value = 0
end)

Add the value when touched:

local Players = game:GetService("Players")

local part = script.Parent

local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local player = Players:GetPlayerFromCharacter(partParent)
	local leaderstats = player and player:FindFirstChild("leaderstats") -- Must match the folder name.
	local value = leaderstats and leaderstats:FindFirstChild("Clicks") -- must match the IntValue name.

	if value then
		value.Value += 10
	end
end

part.Touched:Connect(onPartTouch)

Location to put:
Screenshot 2024-06-27 132743
(NOTE: both script mus be server scripts!)

I don’t have leaderstats but I have a int value or a number value in Players.{player}.Data.PlayerData.Currency.Value and there is where u gain currency when u touch it. i am also using simulator generator so if u use that you will notice where currencys are

So you mean… you don’t use leaderstats for currency?