Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

function keeps repeating, unsure how to stop it?

Hello! I just started scripting yesterday, so this may be a semi-easy question to answer. Either way, I appreciate the help :)

I am trying to create a part "floor" by stepping on a part "step", which I have been successful in. The issue comes where the script continues to make the same part repeatedly for every touch on "step". I have tried two different ways to script it over the past few days and have consulted a large handful of videos, but can not get a solution.

Below is what I currently have scripted, trying to use a variable(I believe that's what it's called) to no longer allow it to create more blocks after "step" has been touched once (Untouched = Untouched + 1). It did not work.

local Untouched = 1
if Untouched == 1 then
	game.Workspace.Step.Touched:Connect(function()
		print("cp1")
		local Floor = Instance.new("Part", game.Workspace)
		print("cp2")
		Floor.Size = Vector3.new(5,2,3)
		print ("cp3")
		Floor.Anchored = true
		print("cp4")
		Floor.Position = Vector3.new(0,1,-54.5)
		print("cp5")
		Floor.Color = Color3.new(0.639216, 0.635294, 0.647059)
		print("cp6")
		Floor.Material = Enum.Material.WoodPlanks
		print("cp7")
		Untouched = Untouched + 1
		print("end?")
	end)
end

I have tried using the connection:disconnect option listed on the roblox help website but had no luck from their explanation figuring out how to use it. If anyone has any tips or tricks on what to do to stop "floor" from spawning more than once, I would be eternally grateful.

1 comment

0

You need to add the if statement in the touch function so it can check once its touched

3 answers

You could use

return

inside the function when you want to stop it, it MUST be inside the function!!

2 comments

0

Also welcome to the #Developer community

0

for some reason it wont let me copy paste the coding, but i removed all mentioned of the untouched variable,including the if/then phrase, replaced cp7 with print("end?") and added return before end). It still continued to spawn extra parts

You can make a debounce variable so that when you touch the part it only runs once.

The reason why your script is running repeatedly is because the Touched function runs when anything touches it. When a player walks over the part, it runs multiple times because a player has many body parts, like the arms and the legs. All your body parts are touching the part, so it runs multiple times.

Try this:

local debounce = true
local Touched = false
while true do -- repeats forever(constantly checking if touched is false or true) and don't forget wait() or your script will crash
wait()
if Touched == false then
    game.Workspace.Step.Touched:Connect(function(hit)
         if hit.Parent:FindFirstChild("Humanoid") and debounce == true then
        print("cp1")
        local Floor = Instance.new("Part", game.Workspace)
        print("cp2")
        Floor.Size = Vector3.new(5,2,3)
        print ("cp3")
        Floor.Anchored = true
        print("cp4")
        Floor.Position = Vector3.new(0,1,-54.5)
        print("cp5")
        Floor.Color = Color3.new(0.639216, 0.635294, 0.647059)
        print("cp6")
        Floor.Material = Enum.Material.WoodPlanks
        print("cp7")
        debounce = false
         wait(2) -- waits 2 seconds until the part registers another touch or if you don't want to code to run anymore, remove this line and debounce = false
         debounce = true
        end
    end)
end
end

Happy scripting!

local Untouched = 1

    game.Workspace.Step.Touched:Connect(function()
	if Untouched == 1 then 
        print("cp1")
        local Floor = Instance.new("Part", game.Workspace)
        print("cp2")
        Floor.Size = Vector3.new(5,2,3)
        print ("cp3")
        Floor.Anchored = true
        print("cp4")
        Floor.Position = Vector3.new(0,1,-54.5)
        print("cp5")
        Floor.Color = Color3.new(0.639216, 0.635294, 0.647059)
        print("cp6")
        Floor.Material = Enum.Material.WoodPlanks
        print("cp7")
        Untouched = Untouched + 1
        print("end?")
		end
    end)

This discussion is preserved as it was. New answers, comments, and votes are closed.