Issue with Collector Behavior in Tycoon Game - Only Owner Should Receive Money

**Hello,

I’m currently working on creating a tycoon game and I’m facing a problem that I can’t seem to solve. The issue is related to the collectors in the game. When the owner of a plot steps on their own collector, they correctly receive the money. However, when another player (who is not the owner) steps on the collector belonging to the owner, the owner still receives the money, which is not the intended behavior.

I have tried implementing various solutions, such as checking the player’s UserId before granting them the money, but so far, I haven’t been successful in resolving the problem. I want to ensure that only the owner of the collector receives the money when they step on it, and no other players should be able to contribute or interfere with the owner’s earnings.

I would greatly appreciate any insights or suggestions on how I can address this issue effectively. Thank you in advance for your help!**

local function getPlot(player)
	for _, plot in game.Workspace.Plots:GetChildren() do
		if plot:GetAttribute('Id') == player.UserId then
			return plot
		end
	end
end

local function collectCash(player)
	local debounce = false
	getPlot(player).Collector.Cash_Collector.Touched:Connect(function(otherPart)
		local char = otherPart.Parent
		local hum = char.Humanoid
		
		if hum and debounce == false and getPlot(player) then
				debounce = true
				local amount = getPlot(player).Collector.Cash_Collector:GetAttribute('Amount')
				player.leaderstats.Cash.Value += amount
				getPlot(player).Collector.Cash_Collector:SetAttribute('Amount', 0)
				local newAmount = getPlot(player).Collector.Cash_Collector:GetAttribute('Amount')
				getPlot(player).Collector.Screen.SurfaceGui.TextLabel.Text = newAmount
		end
	end)
end
3 Likes

The error here is in the get plot function, you are searching trough every plot until the id is the same as the player who touched it

To fix this, get the ID variable of the Owner plot without looping trough every plot, and then check if the id is the same as the player who touched it

2 Likes

Could you write me this script?

I am struggling to understand what you would like to happen. Can you just clarify?

However, when another player (who is not the owner) steps on the collector belonging to the owner, the owner still receives the money, which is not the intended behavior.

Why would you want a player who does not own the tycoon to take the money?

You are not checking the player who touched the collector. Is the ‘player’ variable equal to the owner of the plot? If so, you can use GetPlayerFromCharacter and use a simple if:

getPlot(player).Collector.Cash_Collector.Touched:Connect(function(otherPart)
		local char = otherPart.Parent
		local hum = char.Humanoid
		
		if hum and debounce == false and getPlot(player) then
                local plrThatTouched = game.Players:GetPlayerFromCharacter(char)
                if plrThatTouched ~= player then return end
				debounce = true
				local amount = getPlot(player).Collector.Cash_Collector:GetAttribute('Amount')
				player.leaderstats.Cash.Value += amount
				getPlot(player).Collector.Cash_Collector:SetAttribute('Amount', 0)
				local newAmount = getPlot(player).Collector.Cash_Collector:GetAttribute('Amount')
				getPlot(player).Collector.Screen.SurfaceGui.TextLabel.Text = newAmount
		end
	end)

He dose not mean it like that. hes simply saying he dosn’t want the non owner to collect the money for him and only collect the money when the owner touches the Collector…

1 Like

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