How do I make this collectable item?

Im trying to make sort of an Coin System , where you touch a part and it will give you money and teleport to a random spawn point (part) in a specific folder , I cant make it so it doesn’t touch the part like 10 times (idk how to implement a debounce) , also how do I teleport the part to a random part ?

Code:

local spawnFolder = game.Workspace.SpawnLocations
local Part = game.Workspace.Bottle
local touched = false

Part.Touched:Connect(function(hit)
	if touched == false then
		wait(.5)
		touched = true
	local randomNum = math.random(1,#spawnFolder:GetChildren())
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player then
		for obj,value in pairs(spawnFolder:GetChildren()) do
			print(randomNum)
			if value == randomNum then
					print(randomNum)
				end
			end
		end
	end
end)

I am strugling to add a debounce and also make it teleport to a part In spawnFolder

folder:
image

simple.

	if touched == false then
		touched = true
		wait(.5)

yh but then it doesnt print the random number?

		for obj,value in pairs(spawnFolder:GetChildren()) do
			print(randomNum)
           --let me get this clear, you did it incorrectly, object is second and value is first.
			if obj == randomNum then
					print(randomNum)
				end
			end

The debounce should be set to true before the script executes and only disabled after it executes. What you’re doing is disabling the debouce even before the script runs, which is meaningless. The reason is that wait() yields the current thread (which in this case is your code), meaning that the entire script will halt for 1/2 a second and then execute.

Also, you’re comparing a random number and a SpawnLocation. Your code will never print because that will never, ever be true. Compare the name of the spawn location (as a number) to the value instead.

Additionally, use Random.new() instead of math.random(), and use task.wait() instead of just wait(). Reason being Random.new() is an improvement over math.random() when it comes to random numbers, and task.wait() is an improved version of wait() (which is scheduled to be deprecated).

Do this instead:

local touched = false
local rand = Random.new()

Part.Touched:Connect(function(hit)
  if not touched then
    -- Set the debounce
    touched = true
    local randomNum = rand:NextInteger(1, #spawnFolder:GetChildren())
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
      for obj, value in pairs(spawnFolder:GetChildren()) do
        print(randomNum)
        -- Check if the name of the spawn location is equal to our random number
        if tonumber(value.Name) == randomNum then
          print(randomNum)
        end
      end
    end
    -- Only reset the debounce after the script executes
    task.wait(0.5)
    touched = false 
  end
end)

Let me know if there’s an error; I wrote this very quickly and without tests

1 Like

Based on that I managed to make it work perfectly , thank you!

Final code:

local touched = false
local rand = Random.new()
local Part = game.Workspace.Bottle
local spawnFolder = game.Workspace.SpawnLocations

Part.Touched:Connect(function(hit)
	if not touched then
		touched = true
		local randomNum = rand:NextInteger(1, #spawnFolder:GetChildren())
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player then
			for obj, value in pairs(spawnFolder:GetChildren()) do
				if tonumber(value.Name) == randomNum then
					Part.CFrame = value.CFrame
					player:WaitForChild("Bottle").Value += 1
				end
			end
		end
	end
	task.wait(0.5)
	touched = false
end)
1 Like

Great!

Move that task.wait(0.5); touched = false block to be in the scope of the starting if statement. I made an error; I’ve fixed my code, please refer to it

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