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)
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.