I made a script that creates lasers and reflects them off of parts not in an ignore list. It works find in a part in workspace using a normal script, but when I try to implement it in a tool with a local script, it will just stop at the first point of impact. It seems that in the script, using a print with the count variable (print(count)), it will increment as many times as it should, but in the local script it only increments once every time you click. Why? I only modified the script slightly? Heres the code (I know its a lot I dont know where the problem is.):

Here is the normal script code:

wait(5)

count = 0
maxCount = 10

ignoreList = game.Workspace.Glass:GetChildren()

table.insert(ignoreList, script.Parent)

function shoot(position, normal)
	local ray = Ray.new(position, normal * 500)
	local hit, endPosition, surfaceNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
	
	local beam = Instance.new("Part", workspace)	
	
	beam.BrickColor = BrickColor.new("Bright red")
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = 0.25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	
	local distance = (position - endPosition).magnitude
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(position, endPosition) * CFrame.new(0, 0, -distance / 2)
	
	if (hit) and count < maxCount then
		count = count + 1
		print(count)
		local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
		local newNormal = reflectedNormal
		shoot(endPosition, newNormal)
	end
end

shoot(script.Parent.CFrame.p, script.Parent.CFrame.lookVector)

Here is the normal script code:

local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local canUse = true

count = 0
maxCount = 10

ignoreList = game.Workspace.Glass:GetChildren()

table.insert(ignoreList, script.Parent)
table.insert(ignoreList, script.Parent.Parent.Handle)

tool.Equipped:connect(function(mouse)
	
	mouse.Button1Down:connect(function()
		shoot(tool.Shoot.CFrame.p, (mouse.Hit.p - tool.Shoot.CFrame.p).unit)
	end)
end)

function shoot(position, normal)
	if canUse then
		canUse = false
		local ray = Ray.new(position, normal * 300)
		local hit, endPosition, surfaceNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
			
		local beam = Instance.new("Part", workspace)	
			
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
			
		local distance = (position - endPosition).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(position, endPosition) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 1)
		
		if (hit) and count < maxCount then
			count = count + 1
			print(count)
			local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
			local newNormal = reflectedNormal
			shoot(endPosition, newNormal)
		end
		
		wait(1)
		
		canUse = true
	end
end

2 comments

0

You can’t use the Button1Down event in a normal script. Change to LocalScript.

0

Sorry the second script is in a local script I wrote it wrong.