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

How to activate this script on touch and disabling it after a certain amount of time?

I am having trouble making this script activate on touch and after a certain of amount time disable it. What is this script supposed to do? I am trying to activate this script on touch after the player has touched a certain part (thats not the model) the model will move and after a certain amount of seconds the script will be disabled and the model will stop moving. I tried breaking but I am not sure if I even used it correctly. help plz

script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then

workspace.TWO.PrimaryPart = workspace.TWO.HumanoidRootPart

while true do
    for i = 1,100 do 
        wait()

                workspace.TWO:SetPrimaryPartCFrame(CFrame.new(workspace.TWO.PrimaryPart.Position + Vector3.new(0,0,1)))
                wait(4)
                break
              end
        end
    end
end)

1 comment

0

well did it work when you broke it?

1 answer

This should work:

local DB = false
script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
		workspace.TWO.PrimaryPart = workspace.TWO.HumanoidRootPart
		if DB == false then
			DB = true 
			for i = 1,100 do 
				if i ~= 100 then --Checks to see whether or not i is equal to 100
				    workspace.TWO:SetPrimaryPartCFrame(CFrame.new(workspace.TWO.PrimaryPart.Position + Vector3.new(0,0,1)))
				    wait(4)
				else
					script.Disabled = true --Disables the script if i is equal to 100
				end
		   end
			else
		end
    end
end)

You should learn more about debounce, it's really useful in times like these.

Info about denounce: https://developer.roblox.com/en-us/articles/Debounce

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