(SOLVED) Player Cant Buy Area

About

Ok so I am making a game that is a mix of snow shoveling simulator and lumber tycoon 2 and there is a feature where you can buy different areas.

How it should work

When the part is touched then if they have enough money the part will destroy locally.

The problem

Everything works except after one player buys the area the other person cant buy it. There were no errors.

The code

Local script in starter player scripts
local debounce = false

game.Workspace.OrangeBorder.Touched:Connect(function(hit)

if debounce == false then
	debounce = true

if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player ~= game.Players.LocalPlayer then 
	return 
else
game.ReplicatedStorage.DestoryBarrier.OrangeTree:FireServer("OrangeBorder", 100)
print("Server Fired!")
wait(1)
debounce = false
	end end
end

end)

game.ReplicatedStorage.DestoryBarrier.OrangeTree.OnClientEvent:Connect(function(border)
print(“Approved”)
print(border)
game.Workspace[border]:Destroy()
end)

Something was weird with the formatting ^^

Script in server script service

game.ReplicatedStorage.DestoryBarrier.OrangeTree.OnServerEvent:Connect(function(player, border, price)
print("Recived!")
print(player, border, price)
print(player.leaderstats.Coins.Value)
if player.leaderstats.Coins.Value >= price then
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
	game.ReplicatedStorage.DestoryBarrier.OrangeTree:FireClient(player, border)
end

end)

Thank you for reading! :smile:

2 Likes

Oh! It took me a while but it looks like your issue is because you set Debounce to true if someone else hits it, and never sets it back to false.

1 Like

I set it back to false. I dont think it is the debounce as I had a problem without the debounce and after adding the debounce it fixed it. But thanks for trying.

This may not be the problem because the denounce sets back to false after the wait. I would think that it would be to set the debounce to false if a non-player object hit it before the return.

1 Like

Hello,
I have a few things to say about your code, and could potentially solve your issue.

First, you’re checking if the debounce is false, then setting it to true. After that, you are checking if the player is the LocalPlayer (which you should use if hit.Parent == localplayer.character). You set the debounce inside of those two if statements, therefore the debounce wouldn’t be set to false after it is done checking. You should move the wait(1) debounce = false to outside of both of the if statements but in the same if statement where you’re setting the debounce to true.

This is what I mean:

if debounce == false then
	debounce = true
	
	if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player ~= game.Players.LocalPlayer then 
			return 
		else
			game.ReplicatedStorage.DestoryBarrier.OrangeTree:FireServer("OrangeBorder", 100)
			print("Server Fired!")
        end
	end
	wait(1)
	debounce = false --have this outside so the debounce will be reverted correctly
end

Another thing you should keep in mind is to never trust the client, as you’re checking the price on the client instead of checking the price on server. If you really want to use the client to calculate if the player has enough to buy that border, also do a server check to see if anything is off.

2 Likes

This would be the proper way of doing it. Kudos to you for mentioning the client sided check. I would see why it would stop functioning because the debounce is still true when a non-player object hits it . Most likely, when a player area has loaded in, it hit the border and the debounce stayed true.

2 Likes

Yea it gets set back to false, but thats only if the local player hit it. In the specific case which breaks the code, the other player hits it. Which will have it set to true on all clients, and never get set back to false.
ZQ86’s solution should fix the problem.

Wait you might be right sorry for the confusion I will fix the code.

Make sure to mark the solution to make sure other people aren’t wasting their time to find a post that already fixes the problem.

2 Likes

So I added the code you wanted me to but it still seems to not work. I am wondering if there is some external factor in play. Here is how I added it.
local debounce = false

game.Workspace.OrangeBorder.Touched:Connect(function(hit)

if debounce == false then
debounce = true

if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player ~= game.Players.LocalPlayer then 
		return 
	else
		game.ReplicatedStorage.DestoryBarrier.OrangeTree:FireServer("OrangeBorder")
		print("Server Fired!")
    end
end
wait(1)
debounce = false --have this outside so the debounce will be reverted correctly

end
end)

game.ReplicatedStorage.DestoryBarrier.OrangeTree.OnClientEvent:Connect(function(border)
print(“Approved”)
print(border)
game.Workspace[border]:Destroy()
end)

hmm Something still isnt working. Is it possible somethin external is in play?

The return breaks (not breaks the script) the function and therefore it is still set to true. You would want to move that before the return and after the wait making two of these.

2 Likes

Thank you so much @polill00, @TriIIionaires, @zQ86 for the help!

3 Likes

It would be reasonable to mark the actual solution by the way if any other people have the same problem.

2 Likes