How to make a coin can be picked up by the player who made more clicks?

Hi, I have a script so that after 10 clicks on a block, a coin will drop out and it will add coins to the player. Now any player can pick up a coin, how to make that the dropped coin can be picked up only by the one who made more clicks?

Script for that the coin drops out after 10 clicks

local coal = script.Parent
local coal1 = script.Coal5
local coal2 = script.Coal6
local coal3 = script.Coal7
local coal4 = script.Coal8
local click = coal.ClickDetector
local clickAmount = 0
local canClick = true


local function addClick(player)
	if canClick then
		player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + player.leaderstats.Level.Value
		clickAmount = clickAmount + player.leaderstats.Level.Value
	
	end
		if clickAmount >= 10 then
			local num = math.random(1,2)	
				if num == 1 then
				local toast=script.Parent.toast:clone()
				
				toast.Parent=game.Workspace
				toast.Transparency=0
				toast.Locked=false
				toast.Anchored=false
				toast.CanCollide=true
				
			local offsetCFrame = CFrame.new(0, 3, 0) * CFrame.Angles(0, 0, 0)
				toast.CFrame = coal.CFrame:ToWorldSpace (offsetCFrame)
				print("Hello")
				
			
				
				
				elseif num == 2 then
				local toast=script.Parent.toast:clone()

				toast.Parent=game.Workspace
				toast.Transparency=0
			toast.Locked=false
				toast.Anchored=false
				toast.CanCollide=true

			local offsetCFrame = CFrame.new(0, 3, 0) * CFrame.Angles(0, math.rad(90), 0)
				toast.CFrame = coal.CFrame:ToWorldSpace (offsetCFrame)
				print("Helloo")	
				
				
				end
			
			clickAmount = 0
			canClick = false
			coal1.Transparency = 1
			coal2.Transparency = 1
			coal3.Transparency = 1
			coal4.Transparency = 1
			wait(3)
			coal1.Transparency = 0
			coal2.Transparency = 0
			coal3.Transparency = 0
			coal4.Transparency = 0
			canClick = true
			

			
			

		end
end


	click.MouseClick:Connect (addClick)

Script to collect cash:

script.Parent.CanCollide = false
local cloning
db = false

script.Parent.Touched:Connect(function(hit)
	cloning = script.Parent:Clone()
	script.Parent.Transparency = 0
	local humanoid = game.Players:GetPlayerFromCharacter(hit.Parent)
	if humanoid then
		if not db then
			db =true
			humanoid.leaderstats.Coins.Value = humanoid.leaderstats.Coins.Value + math.random(10,50)
			wait()
			for i = 1,1,1 do
				script.Parent.Transparency = 1
				wait()
				print(i)
			end
			script.Parent:Destroy()
			wait(3)
			db = false
		end
	end
	
end)
1 Like

Or that coin was taken by the player who made the first click

You can achieve this through this use of a “RemoteEvent” instance which fires the client, the coin which you have named “toast” is then created in a local script and can only be seen/collected by the player which performed the tenth click.

--SERVER SCRIPT--
local coal = script.Parent
local coal1 = script.Coal5
local coal2 = script.Coal6
local coal3 = script.Coal7
local coal4 = script.Coal8
local toasts = script.Parent.toast
local click = coal.ClickDetector
local clickAmount = 0
local canClick = true
local storage = game:GetService("ReplicatedStorage")
local re = storage:WaitForChild("RemoteEvent")

local function addClick(player)
	if canClick then
		player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + player.leaderstats.Level.Value
		clickAmount = clickAmount + player.leaderstats.Level.Value
	end
	if clickAmount >= 10 then
		local num = math.random(1,2)	
		if num == 1 then
			re:FireClient(player, num)
		elseif num == 2 then
			re:FireClient(player, num)
		end
		clickAmount = 0
		canClick = false
		coal1.Transparency = 1
		coal2.Transparency = 1
		coal3.Transparency = 1
		coal4.Transparency = 1
		task.wait(3)
		coal1.Transparency = 0
		coal2.Transparency = 0
		coal3.Transparency = 0
		coal4.Transparency = 0
		canClick = true
	end
end

click.MouseClick:Connect(addClick)
--LOCAL SCRIPT--
local player = game.Players.LocalPlayer
local storage = game:GetService("ReplicatedStorage")
local re = storage:WaitForChild("RemoteEvent")
local toasts = workspace:FindFirstDescendant("Toast") --change name if not correct
local coal = workspace:FindFirstDescendant("Coal") --change name if not correct

re.OnClientEvent:Connect(function(num)
	if num == 1 then
		local toast = toasts:clone()
		toast.Parent = workspace
		toast.Transparency = 0
		toast.Locked = false
		toast.Anchored = false
		toast.CanCollide = true

		local offsetCFrame = CFrame.new(0, 3, 0) * CFrame.Angles(0, 0, 0)
		toast.CFrame = coal.CFrame:ToWorldSpace (offsetCFrame)
	elseif num == 2 then
		local toast = toasts:clone()
		toast.Parent = workspace
		toast.Transparency = 0
		toast.Locked = false
		toast.Anchored = false
		toast.CanCollide = true

		local offsetCFrame = CFrame.new(0, 3, 0) * CFrame.Angles(0, 0, 0)
		toast.CFrame = coal.CFrame:ToWorldSpace (offsetCFrame)
	end
end)

Place the local script inside the “StarterPlayerScripts” folder or the “StarterCharacterScripts” folder. The “RemoteEvent” instance you need is named “RemoteEvent” place it inside the “ReplicatedStorage” folder.

image

You may also need to change the name of “Coal” and “Toast” in the local script I created (make sure they are correctly referenced.