Issue with script please help

I was making a buy area system for my simulator game but it takes away way too much money and I figured out why, can anyone help me find a solution? (Print statement marks where it is wrong)

for i, v in pairs(teleporters:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(hit)
			local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
			
			if humanoidRootPart then
				local char = hit.Parent
				local player = game.Players:GetPlayerFromCharacter(char)
				
				if player.Areas:FindFirstChild(player.CurrentWorld.Value):FindFirstChild(v.Name).Value == true then
					humanoidRootPart.CFrame = floors:FindFirstChild(v.Name.."Floor").CFrame + Vector3.new(0, 10, 0)
				else
					print("Hi") --Prints the more I step on the part
					eventFolder.AskToBuy:FireClient(player, floors:FindFirstChild(v.Name.."Floor").Cost.Value, v.Name)
				end
			end
		end)
	end
end

Apply the concept of debounces.

1 Like

was it because you didn’t add a cooldown or debounce to the button?

It still does not work, it does it way less but still it wont work

local debouce = false

for i, v in pairs(teleporters:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(hit)
			local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
			
			if humanoidRootPart then
				local char = hit.Parent
				local player = game.Players:GetPlayerFromCharacter(char)
				
				if player.Areas:FindFirstChild(player.CurrentWorld.Value):FindFirstChild(v.Name).Value == true then
					humanoidRootPart.CFrame = floors:FindFirstChild(v.Name.."Floor").CFrame + Vector3.new(0, 10, 0)
				else
					if debouce == false then
						debouce = true

						eventFolder.AskToBuy:FireClient(player, floors:FindFirstChild(v.Name.."Floor").Cost.Value, v.Name)

						wait(1)

						debouce = false
					end
				end
			end
		end)
	end
end

Is it supposed to take your money away only once?

yea, It is taking it away mutiple times tho

In that case, change the :Connect to :Once

Once I buy it i want to be able to touch it again to teleport so :Once wont work

The debounce system is the right way to go here, not entirely sure if this will work but try this

local debouce = false

for i, v in pairs(teleporters:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(hit)
			local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")

			if humanoidRootPart then
				local char = hit.Parent
				local player = game.Players:GetPlayerFromCharacter(char)

				if player.Areas:FindFirstChild(player.CurrentWorld.Value):FindFirstChild(v.Name).Value == true then
					humanoidRootPart.CFrame = floors:FindFirstChild(v.Name.."Floor").CFrame + Vector3.new(0, 10, 0)
				else
					if debounce == false then
						debounce = true
						eventFolder.AskToBuy:FireClient(player, floors:FindFirstChild(v.Name.."Floor").Cost.Value, v.Name)
					end
				end
			end
		end)
		v.TouchEnded:Connect(function()
			task.wait(.5)
			debounce = false
		end)
	end
end

It was something to do with another script, thanks for the help tho!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.