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

How to change a block size once hit?

I made a laser but once it goes over a block I don't want it to go through.

--[[
	Make a variable to hold where to start the laser, one how wide each platform is, one for the space
	between the platforms, and one for the end
	]]--

-- Start --
local start = 65.201
-- Holder --
local holder = start
-- Width --
local width = 12.3
-- Space --
local space = 6
-- No. platforms --
local number = 4
-- End --
local endP = 132.201
-- Margin --
local margin = .16
-- Counter --
local counter = 0
-- forward --
local forward = true
-- height --
local h = 10

-------------------------------------
local deBounce = false
script.Parent.Changed:connect(function(change)
	if deBounce == false then
		deBounce = true
		if change == "Position" then
			if forward == true then
				-- Check current position
				local doStuff = isInMargin(script.Parent.Position.X, start)
				if doStuff then
					--print("Got margin")
					script.Parent:Resize(1, -h)
					doStuff = false
				end
				
				-- Go down if it's reached the end of the platform
				local goDown = isInMargin(script.Parent.Position.X, width + start)
				if goDown then
					script.Parent:Resize(1, h)
				--	print("Got down firing")
					goDown = false
					counter = counter + 1
					if counter == number then
						forward = false
						counter = number
						start = endP
						wait(.1)
					else
						start = script.Parent.Position.X + space
					end
					--print(counter)
				end
			elseif forward == false then
				-- Check current position
				--print("Backward")
				local doStuff = isInMargin(script.Parent.Position.X, start)
				if doStuff then
					counter = counter - 1
					--print("Got margin(b)")
					script.Parent:Resize(1, -h)
					if counter <= -1 then
						start = holder
						forward = true
						--print(start)
						counter = 0
						wait(.2)
					end
					doStuff = false
				end
				
				-- Go down if it's reached the end of the platform
				--print(script.Parent.Position.X .. ", " .. start-width)
				local goDown = backward(script.Parent.Position.X, start - width)
				--print("Checked goDown")
				if goDown then
					script.Parent:Resize(1, h)
					--print("Got down firing(b)")
					start = script.Parent.Position.X - space
					if counter == 0 then
						start = holder
						forward = true
						wait(.2)
					end
					goDown = false
					--print(counter.. " (b)")
				end
				--print("Past goDown")
			end
		
		end
		deBounce = false
	end
end)
--------------------------------------

--[[
	Get the current number and see if it + margin is greater than or equal to the start position, if it is, check
	to see if start position + margin is less than current position. If start position + margin < current position
	then don't do anything. 
--]]

function isInMargin(num1, m)
	if num1 + margin >= m then
		if m + margin >= num1 then
			--print('Returning true')
			return true
		end
	else
		--print('Returing false')
		return false
	end
end

function  backward(num, num2)
	--print("Got asked with " .. num .. ", " .. num2)
	if num - margin <= num2 then
		if num2 - margin <= num then
			return true
		end
	end
end

sorry its a long script

1 comment

0

You can use the Touched event, you should also use raycasting. Raycasting will automatically stop once it hits an object.

1 answer

✓ Accepted answer

You can use the Touched event and anchor the laser. Here would be how you use it. Insert this script in the laser.

script.Parent.Touched:connect(function(_)
	print("Something has touched the laser!")
	--code
end)

Insert the rest of the code and you should be fine!

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