When touching part it adds +1 to Everyones leaderstat, i want it to only add +1 to the person who touched it

When touching part it adds +1 to Everyones leaderstat, i want it to only add +1 to the person who touched it


local Buy = game.Workspace.Buy
local BuyValue = Buy.Value.Value

game.Players.PlayerAdded:Connect(function(joining) --When the player joins the game…

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = joining

local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 1
Coins.Parent =  leaderstats


BuyValue = 200


local Part1 = game.Workspace.Part1
Buy.Touched:Connect(function(touched)
	local humanoid = touched.Parent:FindFirstChild("Humanoid")
	
           if humanoid and Coins.Value >= 200 then
			Buy:Destroy()
			Part1.Transparency = 0
			
			local TotalScore = Coins.Value - BuyValue --Value - Part value 
			print(TotalScore)
		Coins.Value = TotalScore
		
	else
		Buy.BrickColor = BrickColor.Random()
		end

end)

local CollecterCashButton = workspace.Collector
local CashValueScreen = workspace.Collector.CashValueScreen.SurfaceGui.TextLabel
local CashScreen = 0
 

while true do
	
	CollecterCashButton.Touched:Connect(function(touch)
		local humanoid = touch.Parent:FindFirstChild("Humanoid")
		if humanoid then
			Coins.Value = Coins.Value + CashScreen
			CashScreen = 0

		end
	end)
3 Likes

You can format the document to position code lines correctly, making it easier for readers to follow the script. Follow these steps [Shown in IMG]:

1) Right-click on the code
2) Choose “Format”
3) Click “Format Document”


Also, please post the code correctly next time. Thank you.

Now, about your code, if I understand what you are trying to achieve here, then:

local Buy = game.Workspace.Buy
local BuyValue = 200 -- Adjusted to a fixed value

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Value = 0 -- Set starting coins to 0
    Coins.Parent = leaderstats

    Buy.Touched:Connect(function(hit)
        local humanoid = hit.Parent:FindFirstChild("Humanoid")

        if humanoid then
            if Coins.Value >= BuyValue then
                Buy:Destroy()
                local Part1 = game.Workspace.Part1
                Part1.Transparency = 0

                local TotalCoins = Coins.Value - BuyValue
                print(TotalCoins)
                Coins.Value = TotalCoins
            else
                Buy.BrickColor = BrickColor.Random()
            end
        end
    end)

    local CollectorCashButton = workspace.Collector
    local CashValueScreen = CollectorCashButton.CashValueScreen.SurfaceGui.TextLabel
    local CashScreen = 0

    CollectorCashButton.Touched:Connect(function(hit)
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            Coins.Value = Coins.Value + 1 -- Adjusted to always add 1 when touched
            CashScreen = 0
        end
    end)
end)

This code is what you are looking for:

2 Likes

Hey so basically does that add +1 to the person that touches it only, because my other code added +1 to everyone

1 Like

Yeah, it does! Sorry about the edits in my reply; I was trying to make it more understandable.

1 Like

Cleaned up your code a bit here. It’s especially bad to set up the .Touched connection inside your loop, because it will eat up memory and fire hundreds or thousands of touched events when the collector button is stepped on (depending on how many times the loop has run so far).

I also added debounces to avoid processing events too quickly, especially if deferred signals is enabled.

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local part1 = Workspace.Part1
local collectCashPart = Workspace.Collector
local cashValueLabel = collectCashPart.CashValueScreen.SurfaceGui.TextLabel
local buyPart = Workspace.Buy
local buyCost = buyPart.Value.Value

local function getPlayerFromPart(part)
	if not part:IsDescendantOf(Workspace) then
		return
	end

	while part and part.Parent and part.Parent ~= Workspace do
		if part.Parent:IsA("Model") then
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				return player
			end
		end

		part = part.Parent
	end
end

Players.PlayerAdded:Connect(function(joiningPlayer)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = joiningPlayer

	local coinsLeaderstat = Instance.new("IntValue")
	coinsLeaderstat.Name = "Coins"
	coinsLeaderstat.Value = 1
	coinsLeaderstat.Parent = leaderstats

	local wasTouched = false

	-- This sets up a connection on the same buyPart as everyone else. Somehow you need to
	-- reference an unused buyPart unique for this player. Without seeing the setup of your game,
	-- I can't tell you how to do that.
	buyPart.Touched:Connect(function(touchingPart)
		if wasTouched then
			return
		end

		wasTouched = true

		local player = getPlayerFromPart(touchingPart)

		if not player then
			wasTouched = false
			return
		end

		if player and coinsLeaderstat.Value >= buyCost then
			buyPart:Destroy()
			part1.Transparency = 0
			coinsLeaderstat.Value -= buyCost
			print(`{player.DisplayName} bought the part for {buyCost}!`)
		else
			buyPart.BrickColor = BrickColor.Random()
			task.delay(0.5, function()
				wasTouched = false
			end)
		end
	end)

	local storedCash = 0
	local collectCashDebounce = false

	-- Again this is the same collect cash part for everyone, typically tycoon games have each player
	-- have their own collection part.
	collectCashPart.Touched:Connect(function(touchingPart)
		if collectCashDebounce then
			return
		end

		local player = getPlayerFromPart(touchingPart)

		if not player then
			return
		end

		-- If this were unique to the player, you'd need to check that the touching player is the owner with something like:
		-- if player ~= joiningPlayer then
		-- 	return
		-- end

		collectCashDebounce = true

		coinsLeaderstat.Value += storedCash
		storedCash = 0

		task.delay(0.5, function()
			collectCashDebounce = false
		end)
	end)

	while true do
		task.wait(1)
		storedCash += 1
		cashValueLabel.Text = storedCash
	end
end)

It’s unclear to me how this is meant to work because you have a static reference to a buy part that is used for everyone. The connections are not checking for the joining player to be the one to touch, it’s just allowing a touch from any player. This is probably the issue you’re experiencing.

Please read the comments in here that I added for a little explanation of the issue.

1 Like

Ok thank you so much! Im trying to figure, what has been changed to do that?

1 Like

I fixed up code, by making it look better, and for it to work more smoothly, I also changed the script of the “CollectorCashButton” a little bit, now it adds + 1 to the coins value. you had while loop, and I removed it I don’t understand why you needed it.

I hope this doesn’t come off as rude, but I found your code a bit unclear. I gave it a shot and made some adjustments based on my understanding.

1 Like

It wasn’t shown in OP’s code snippet, but I inferred that the loop’s purpose is to increase stored cash over time, and stepping on the collection button resets the store to 0 and adds it to your coin leaderstat. See my snippet for a more complete implementation of this.

1 Like

Thanks! So why was it adding +1 to everyone and what did u do to stop it? Sorry im very new to coding

1 Like

oh yeah, I had hard time understanding what was written in the code and what they wanted to achieve, but you got it right.

1 Like

My code is very unclear as this is the first code i think ive ever done based on a game

1 Like

So @LuckyPlanetsAlt1 the unresolved issue is:

  • Is the player meant to have their own unique tycoon area?
    If so, then you need to get references to unique purchase and collect buttons instead of using the same one for everyone. Can you explain a bit about how your game is set up in this regard?
1 Like

yes don’t worry about it. don’t take any of this in a bad way, I am actually glad that you are trying, and for a first ever code done based on a game its not that bad.

1 Like

I will let you handle it from here since you understand the code better, I gotta go best of luck to ya.

1 Like

Ok, so

  1. each person needs a seperate tycoon spot
  2. Im going do that later
  3. So, as the image attached here, i want 4 spots. If a player touches the Green pad which gives the money, i want it to give it to them only. Before, when somebody touched it, it gave the coins to everyone
    image

Also was wondering the screen isnt adding +1

You’ll need a concept of an “owner” of a plot then. I highly recommend following a tutorial like this one https://www.youtube.com/watch?v=XY6Ig48tAnk which covers in detail how to set up a basic tycoon and track the owner.

Ok thanks but for other games like simualtors, how would i make it so 1 person who picks up a coin gets it not everyone

If I understand the code correctly, it’s not everyone getting the coins, it’s anyone. That is, the problem is there is no check that the person touching the collection button is the same person who owns the tycoon plot, because there’s no concept of an owner in this game.

That tutorial I linked keeps track of owners by setting an attribute on the tycoon plot model. There’s a number of different ways you could track ownership of a plot, and that is a perfectly good way to do it. So first you need to track who owns the plot, then add a check during the touch that doesn’t process giving coins if the touching player is not the owner.

Its everyone thats getting the coins