This code is suppose to allow a player to create a rectangle by clicking, and then dragging to make the end of the rectangle follow their mouse and change size and angle to follow the mouse on the X, Z axis's. Then when they let go of their mouse, it shoots outwards. It works the first time, but if I try to do it again it can either not appear, create 2 of the object (or shake so much that it looks like 2, not sure), or not shoot out as far as it should or at the correct angle. Why is this happening?
local LocalPlayer = game.Players.LocalPlayer
local mouse = LocalPlayer:GetMouse()
local UserInputService = game:GetService("UserInputService")
local clicking = false
local boolean = false
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
clicking = false
local beamLength = beamIndicator.Size.Z
local direction = beamIndicator.CFrame.LookVector
for i = 1, 100 do
if i <= 30 then
beamIndicator.Size = beamIndicator.Size - Vector3.new(-0.5, -0.5, 1 * beamLength / 10)
end
beamIndicator.CFrame = beamIndicator.CFrame - Vector3.new(direction.X * beamLength / 15, direction.Y * beamLength / 15, direction.Z * beamLength / 15)
wait()
end
beamIndicator:Destroy()
wait()
boolean = false
print("Loop Ended")
end
end)
UserInputService.InputBegan:Connect(function(input)
print("Input Started")
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if boolean == true then return end
boolean = true
print("Clicked")
clicking = true
local StarterMousePosition = mouse.hit
beamIndicator = Instance.new("Part", workspace.Projectiles)
beamIndicator.Name = "BeamIndicator"
beamIndicator.Anchored = true
beamIndicator.Material = Enum.Material.Neon
beamIndicator.CanCollide = false
while clicking == true do
wait()
local CurrentMousePosition = mouse.hit
local aimer = -(Vector3.new(StarterMousePosition.X, 3, StarterMousePosition.Z) - Vector3.new(CurrentMousePosition.X, 3, CurrentMousePosition.Z))
local distance = aimer.magnitude
if distance >= 100 then distance = 100 end
beamIndicator.Size = Vector3.new(1, 1, distance)
beamIndicator.CFrame = CFrame.new(Vector3.new(StarterMousePosition.Position.X, 3, StarterMousePosition.Position.Z), Vector3.new(CurrentMousePosition.Position.X, 3, CurrentMousePosition.Position.Z)) * CFrame.new(0, 0, -distance / 2)
print("Success")
end
end
end)

6 comments
btw, that's cool
Don't set the parent of the object in the Instance.new(), it has been deprecated for many years and is a bad practice unless you aren't planning on setting any of the object's properties.
Also, in InputEnded, what is beamIndicator? I don't see you set that variable anywhere before you use it or outside of the function.
beamIndicator is the object being created in the function that runs first. I figured that by the time the InputEnded function had run, the variable would already be set because the first function had run, but maybe this isn't the case? Not sure. Also, wow I had no idea that assigning the parent like that was deprecated!
The whole thing works correctly the first time I assumed that wasn't the problem..
Never mind, It has been fixed.