How would I go about giving the player money?

So what I’m trying to do is that when the player touches a part, they get money, but it only does it once, so that the player can’t just spam touch the part.

Basically, explain to me how to make it so that the player, when they touch the part it gives them money for the first time they touch said part.
Edit: I should have provided my code, sorry!
Code:

local Players = game:GetService("Players")
script.Parent.Touched:Once(function(hit)
	local plr = Players:GetPlayerFromCharacter(hit.Parent)
	plr.leaderstats.Bits.Value += 10
end)
1 Like

Maybe you could hold a table containing all the players who have already had collected the money?

local part = script.Parent
local rewardedPlayers = {}

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    
    if player and not rewardedPlayers[player.UserId] then
        rewardedPlayers[player.UserId] = true
         -- put the code that gives the player money here
    end
end)

And then after some time, it removes the player from the table so it allows them to collect the money again?

1 Like

@preetbajwa42 had a good example but id also like to say you can use a boolvalue like “HasClaimed”

Check:

if HasClaimed == false then

if it is reward the player and set it to true:

plr.Coins += RewardAmount
HasClaimed = true

That way, if its ever true, the plr will not be rewarded

what happens if a player already claimed the reward but then someone else who hasnt decides to go ahead and claim it? the previous player already claimed the reward meaning that that the bool value used to check that someone claimed it is true meaning the player who is going to get the reward wont get it

or maybe i’m just overthinking this too much.

You can just set a bool value as an attribute and assign it to each player so for example you would give then a bool attribute like OneTimeClaim and then set it to false, then when the player collects it sets the attribute to true making it so that the player can’t redeem it for the rest of the session and you can always change the attribute whenever.

2 Likes

Nah ur probably right. It’s my bad cause I wasn’t thinking abt the full picture of a server with multiple players. Ur way is probably much better for this case :slight_smile:

What if there were multiple parts that the player can claim, but can only claim once each part?

You should probably make a data store system and have a Boolean value for each part, if a player claims a part it saves the part even if they leave. in your script check if part is already claimed if so then return end.

local SavedPlayers = {}
local PlusCurrency = 100
local TimerWait = 5

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if not table.find(SavedPlayers,player)  then
			task.spawn(function()
				player.leaderstats.Bits.Value += PlusCurrency

				table.insert(SavedPlayers,player)
				wait(TimerWait)
				local Index = table.find(SavedPlayers,player)
				table.remove(SavedPlayers,Index)
			end)
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.