Apartment Sell Script Giving More Money Than It Should

I Have Apartments In My Game Where Players Can Buy/Sell Them And It Works Just Fine But When The Player Sells Their Apartment Instead Of Getting $250 They Get $750 And The Apartment Costs $500 Here Is My Script:

event.OnServerEvent:Connect(function(player, thing)
	if thing == 'sell' then
		if player:WaitForChild('leaderstats'):WaitForChild('OwnsHome').Value == true then
			if player:WaitForChild('leaderstats'):WaitForChild('OwnsHome'):WaitForChild('HomeName').Value == 1 then
				player:WaitForChild('leaderstats'):WaitForChild('OwnsHome').Value = false
				player:WaitForChild('leaderstats'):WaitForChild('OwnsHome'):WaitForChild('HomeName').Value = 0
				model1.IsOwned.Value = false
				player:WaitForChild('leaderstats'):WaitForChild('Money').Value += 250
			end
		end
	end
end)

I Don’t Know Why It Is Doing This Thanks.

1 Like

add a debounce on the function that fires the server event

2 Likes

I added it but it didn’t do anything

1 Like

what you have should work but like flying said above try a debounce could try something like this also i cleaned up your code abit

local PlayerDebounces = {}  -- holds all player debounces to this remote

event.OnServerEvent:Connect(function(player, thing)
	print('Remote Fired...')   -- use these 2 prints to see what is being sent and how many times
	print(player, thing)
	if PlayerDebounces[player.Name] then return end  -- if already debounce return   
	PlayerDebounces[player.Name] = true  -- set debounce 
	delay(2, function()  -- reset the debounce after certain time
		PlayerDebounces[player.Name] = nil
	end)
	if thing == 'sell' then   -- cleaned up the variables some here
		local Leaderstats = player:WaitForChild('leaderstats')
		local OwnsHome = Leaderstats:WaitForChild('OwnsHome')
		local Money = Leaderstats:WaitForChild('Money')
		if OwnsHome.Value then
			local HomeName = OwnsHome:WaitForChild('HomeName')
			if HomeName.Value == 1 then
				OwnsHome.Value = false
				HomeName.Value = 0
				model1.IsOwned.Value = false
				Money.Value += 250
			end
		end
	end
end)

oh and you can also just reset the debounce at the bottom of the remote event after setting it to true at the top… but if there is an error in the code it will never set back to nil

Oh and the reason for the 2 prints is to see if maybe your client is firing the remote more than once if so you should look into your local scripts

4 Likes

Try this:

local playerDebounces = {}

event.OnServerEvent:Connect(function(player, thing)
	if thing == "sell" then
		local leaderstats = player:WaitForChild('leaderstats')
		
		if leaderstats:WaitForChild('OwnsHome').Value and leaderstats.OwnsHome:WaitForChild('HomeName').Value == 1 then
			if playerDebounces[player] then
				return
			end
			
			playerDebounces[player] = true
			
			leaderstats.OwnsHome.HomeName.Value = 0
			leaderstats.OwnsHome.Value = false
			
			model1.IsOwned.Value = false
			leaderstats:WaitForChild('Money').Value += 250
			
			playerDebounces[player] = nil
		end
	end
end)
1 Like

I Tried This But It Still Gives Me $750 Here is my local script:

local function purchase()
	if not debounce then
		debounce = true
	event:FireServer('sell', player)
		frame:TweenPosition(
			UDim2.new(0.361, 0,0.926, 0),
			'InOut',
			'Quad',
			1,
			false
			)
		task.wait(1)
		frame.Visible = false
		debounce = false
	end
end

Thanks.

I Tried This But It Still Gives Me $750 Here is my local script:

local function purchase()
	if not debounce then
		debounce = true
	event:FireServer('sell', player)
		frame:TweenPosition(
			UDim2.new(0.361, 0,0.926, 0),
			'InOut',
			'Quad',
			1,
			false
			)
		task.wait(1)
		frame.Visible = false
		debounce = false
	end
end

Thanks

did you try this if so what was the full print when you hit purchase?

Also do you have anything local messing with the money value or something else server side or that event connected to another purchase or sale?

1 Like

The Print Commands Are running once but I Do Have A Cash System In Game And When You Pick Up The Cash it Also Bugs Out And Gives More Than What Its Supposed To You Can See In My Other Post Here: Why Is My Cash Pickup Giving More Than The Original Value? - #17 by kdopdaux1803

1 Like

well idk looking at that and you resolved it seems
there does still seem to be something else in play here if you have setup the debounces and only getting 1 print from server then you can only be getting 250 extra for selling unless there is code i don’t know about

1 Like

I know the issue, once again.

Do this:

local MoneyToGive = 250
event.OnServerEvent:Connect(function(player, thing)
	if thing == 'sell' then
		if player:WaitForChild('leaderstats'):WaitForChild('OwnsHome').Value == true then
			if player:WaitForChild('leaderstats'):WaitForChild('OwnsHome'):WaitForChild('HomeName').Value == 1 then
				player:WaitForChild('leaderstats'):WaitForChild('OwnsHome').Value = false
				player:WaitForChild('leaderstats'):WaitForChild('OwnsHome'):WaitForChild('HomeName').Value = 0
				model1.IsOwned.Value = false
				player:WaitForChild('leaderstats'):WaitForChild('Money').Value = MoneyToGive
			end
		end
	end
end)

If it doesn’t work, it means that the sell thing gives an extra 250.

1 Like

It’s actually my post, you know ?