Problem checking if the players is the owner of something

Hello develepers, today i ran into a problem. I’m making a tycoon so, in the “cash register system” i need that only the owner of the tycoon can take the cash. every tycoon has a value called “Owner” it’s a number value and i put the Player’s id in that value.

this is the code for the “cash register”

Players.PlayerAdded:Connect(function(player)
	script.Parent.Parent.Items.CashRecolector.Top.SurfaceGui.TextButton.MouseButton1Down:Connect(function()
		if player.UserId == Values.Owner.Value and Values.Owner.Value == player.UserId then
			if Debounce == false then
				Debounce = true
				
				script.Parent.Parent.Items.CashRecolector.Top.SurfaceGui.TextButton.BackgroundColor3 = Color3.fromRGB(213, 0, 0)
				script.Parent.Parent.Items.CashRecolector.Top.Sound:Play()
				
				player:WaitForChild("leaderstats").Money.Value += Values.CashValue.Value
				wait()
				Values.CashValue.Value = 0
				wait(1)
				Debounce = false
				script.Parent.Parent.Items.CashRecolector.Top.SurfaceGui.TextButton.BackgroundColor3 = Color3.fromRGB(20, 204, 0) 
			 
			end
		else return
		end
	end)
end)

there is a line with an “IF” which says: if player.UserId == Values.Owner.Value and Values.Owner.Value == player.UserId then
here i check if the player who clicks is the owner of the tycoon so, after this, when i test with two people this is what happens:

The player at the left is the owner of the tycoon, he can take the money, no problem
The player at the right isn’t the owner of the tycoon. But as you can see, he can click the cash recolector and can take the money too.

If you need more info please comment.

Thank you!

Hi!
First of, you’re doublechecking here, meaning this is equivalent to

if GroupA == groupa and groupa == GroupA then

end

Change that to this:

local OwnerId = Values:FindFirstChild("Owner")
if OwnerId then
-- Also added a few prints:
	print(Values.Owner.Value, player.UserId)
	if player.UserId == OwnerId then
		--Run your money code
	end
end

Edit: And could you please tell me, why you have all this in a PlayerAdded function? :slight_smile:

Uhh i have all that in a PlayerAdded function because i don’t know if there is a parameter in MouseButton1Down function to get the player

Edit: And sadly your method didn’t work new code:

if Values.Owner then
			if Values.Owner.Value == player.UserId then
-- Here all the code that is in the topic

Hi!
Have you put a ClickDetector into your GUI on the board?

If so do this:

-- Insert this script into your clickDetector, or make 1 script that controls all the boards.
local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function(Player)
	-- Code here
end)

No, i made one script that controls all the boards but i can try with a ClickDetector, brb

Edit: Actually i don’t think it’s a good idea, i mean, the button that you need to press is a TextButton so i can’t insert a clickdetector there

Or you could do this!

  • Place a localscript somewhere on the client.
  • Make so that the localscript runs through all the board-buttons.
  • When they click the buttons, fire a remote to the server, with the boardID.
  • If they’re the owner of the boardID sent, then give them the money.

that’s what i tried mins ago, i don’t know why it doesn’t fire. i made a local script in a folder called scripts inside the TycoonModel

And also, it needs to be in the tycoon if i want it to work with only one script, so i can’t put the local script inside starter player scripts or something else…

Localscripts will only work if they’re on the client or inside the localPlayers character. :slight_smile:

Yep, that’s the problem all the scripts needs to be inside the tycoon model because if i don’t do that, i will need to reference every board of the tycoons. And what i want, is that you only need to copy one and tycoon, move it and that new tycoon that you cloned, works perfectly fine without doing anything. so i can’t do that.

I don’t know what to do actually

I would go with a ClickDetector then :slight_smile:

i tried and that didn’t work :slightly_smiling_face:

What didn’t work? Please post outputs & prints if you’ve made any. :slight_smile: