Player by seat, thinking player is still there on each removal

I have a Server Script that gets the player information from a seat. Whenever the player leaves the seat and sits back down, it removes 25 more tokens than what is should. How would I stop this error from occurring? This is driving me crazy.

local Players = game:GetService("Players")
local seat = script.Parent.Parent.Chair.Seat
local currentPlayer = nil

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
		local character = seat.Occupant.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then 
			print(player.Name.." has sat down")
			currentPlayer = player

local function onClicked(player)
	if currentPlayer == nil then
		print(player.Name.. " LEFT SEAT")
		return
end

	if currentPlayer == player then
		LeverPull()
		if player.leaderstats.Tokens.Value >= 25 then
				player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value - 25
					end
				end	
			end
			Pull1.Click.ClickDetector.MouseClick:connect(onClicked)
			return
		end
	end
	if currentPlayer then 
		print(currentPlayer.Name.." has got up")
		currentPlayer = nil
	end
end)	

This is not the full script.

That’s because you needed to record each player which purchased the seat so that they don’t get charged again in the future.

local Players = game:GetService("Players")
local seat = script.Parent.Parent.Chair.Seat
local currentPlayer = nil
local seatPurchasers = {}

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
		local character = seat.Occupant.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then 
			print(player.Name.." has sat down")
			currentPlayer = player

			local function onClicked(player)
				if currentPlayer == nil then
					print(player.Name.. " LEFT SEAT")
					return
				end

				if currentPlayer == player then
					LeverPull()
					if not table.find(seatPurchasers, player) then
						if player.leaderstats.Tokens.Value >= 25 then
							player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value - 25
							table.insert(seatPurchasers, player)
						end
					end
				end	
			end
			Pull1.Click.ClickDetector.MouseClick:connect(onClicked)
			return
		end
	end
	if currentPlayer then 
		print(currentPlayer.Name.." has got up")
		currentPlayer = nil
	end
end)

Basically, there is a function that happens when the player sits down, the player is not buying the seat. I still want the tokens to be taken away each time the player clicks the button, just not have it multiply each time the player exits and reseats in the seat.

Oh, okay you need that to occur.

1 Like

This works the first time, but when the player exits the seat and reseats, nothing happens when the player clicks the button.

Edit: Or the second time at all, even when not exiting the seat.

Can you provide the full script with the click handler?

All that was needed was a debounce… sorry for the inconvenience. Thank you for helping.