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

how can i make it so the script activates only when the part is touched?

The script is under a brick and I want the script to only activate if someone touches it. and after it finishes running the script lines then it will stop and wait for someone to touch it again. I don't have much experience with scripts so some help would be appreciated.

part = script.Parent
repeat
wait(10)
for i = 1, 200 do -- change 200 to whatever number. 200 = 20 studs up/down, 100 = 10 studs up/down, etc.
part.CFrame = part.CFrame * CFrame.new(0,0.1,0)
wait()
end
wait(5)
for i = 1, 200 do -- change 200 to the one up there ^^^
part.CFrame = part.CFrame * CFrame.new(0,-0.1,0)
wait()
end
until false


1 comment

0

no requests

1 answer

✓ Accepted answer

Code:

local part = script.Parent
local InProgress = false  -- We will use this later to find out if another interval is in progress
local debounce = false   -- Use a debounce to prevent people from spamming it

local function MovePart() -- This is from your code
	for i = 1, 200 do
		part.CFrame = part.CFrame * CFrame.new(0,0.1,0)
		wait()
	end
	wait(5)
	for i = 1, 200 do
		part.CFrame = part.CFrame * CFrame.new(0,-0.1,0)
		wait()
	end
end

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get the player from the character
	
	if player then
		if not debounce then
			if not inProgress then
				InProgress = true  -- It is currently in progress
				debounce = true  -- People can't spam no more
				MovePart()   -- Move the part 
				wait(1)
				InProgress = false -- When it's done, make this variable equal to false
				debounce = false -- Now people can step on it again
			end
		end		
	end
end)

4 comments

0

Dont just give code, explain the code so they can learn.

0

Sorry for not adding it in the first place. I had to remake the whole script for him and also I thought that if he didn't understand scripting as well (in the description) he wouldn't understand my code.

0

it works now!! thank you so much omg!!<3

0

No problem!

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